时间记录:2019-6-27
nginx [engine x]是一个HTTP和反向代理服务器,一个邮件代理服务器和一个通用的TCP/UDP代理服务器。使用的最多的最常见的就是反向代理服务器,首先我们需要知道nginx的基本安装和基本操作,其次了解其如何配置反向代理。nginx分为nginx开源和nginx plus(收费),部分我们需要学习它,从开源版就可以了。
nginx文档(个人找到的比较清楚的)
nginx开源安装(1.17版本)
配置yum源
/etc/yum.repos.d/nginx.repo
--------------------------------------------------------------------------------------------------
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
--------------------------------------------------------------------------------------------------
nginx.repo需要创建
配置完成后执行
yum install -y nginx
启动验证安装
nginx
--------------------------------------------------------------------------------------------------
curl-I 127.0.0.1:80
--------------------------------------------------------------------------------------------------
HTTP/1.1 200 OK
Server: nginx/1.17.1
Date: Thu, 27 Jun 2019 15:06:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 25 Jun 2019 13:30:59 GMT
Connection: keep-alive
ETag: "5d122213-264"
Accept-Ranges: bytes
nginx基本启动等操作
nginx -h
--------------------------------------------------------------------------------------------------
nginx version: nginx/1.17.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
--------------------------------------------------------------------------------------------------
nginx关闭:nginx -s stop/ nginx -s quit
nginx重加载:nginx -s reload
注意:这里是通过进程信号的方式(signal),会在启动时生成 /var/run/nginx.pid 文件,在关闭的时候此文件清除
nginx的服务配置
nginx的配置文件在 /etc/nginx/nginx.conf 下,这里简单配置一个spring boot 的服务代理
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 9090; // 监听的端口,用于访问
server_name 192.168.141.129; // 访问的域名,本机地址
location / {
proxy_pass http://10.0.41.80:8080; //被代理的服务
}
}
include /etc/nginx/conf.d/*.conf;
}
修改配置后重新加载配置启动
nginx -s reload
spring boot 服务,简单的hello
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
--------------------------------------------------------------------------------------------------
application.properties
server.port=8080
server.servlet.context-path=/v1
打开浏览器访问 http://192.168.141.129:9090/v1/hello
hello
总结: 这里只是初步的接触和认识nginx的使用,在实际的使用中需要配置负载均衡,选在负载均衡的均衡方法,还有nginx中的cookie,session等问题,需要深入实践。
时间记录:2019-6-27