1、创建nginx配置文件,修改配置

在~/nginx/conf/下创建nginx.conf⽂件,粘贴下⾯内容
vim nginx.conf

user nginx; #配置运行 nginx 服务器用户
worker_processes 1; # 配置允许生成的 worker process 数

error_log /var/log/nginx/error.log warn; # 配置错误日志的存放路径
pid 	 /var/run/nginx.pid; # 配置 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; # 配置允许 sendfile 方式传输文件
	 #tcp_nopush      on;
	 keepalive_timeout 	65; # 配置连接超时时间
	 #gzip on;
	 include /etc/nginx/conf.d/*.conf; # 配置文件的引入
}

注:所有的所有的所有的指令,都要以**;** 结尾
nginx 每个 worker process 每次调用 sendfile() 传输的数据量的最大值

Refer:
创建port(端口) 80.conf配置文件 ~/nginx/conf.d/80.conf

server { # 虚拟主机相关
	 listen		 80; # 监听的端⼝
	 server_name localhost; # 域名或ip
	 # 地址定向,数据缓存,应答控制,以及第三方模块的配置
	 location / { # 访问路径配置
		 root	 /usr/share/nginx/html;# 根⽬录
		 index  index.html index.htm; # 默认⾸⻚
	 }
	 error_page 500 502 503 504 /50x.html; # 错误⻚⾯
	 location = /50x.html { 
	 	 root	 html;
	 }
}

2、nginx静态网页

端口绑定
修改Nginx 的配置⽂件:~/nginx/config.d/port.conf

server {
	 listen		 81; # 监听的端⼝
	 server_name localhost; # 域名或ip
	 location / { # 访问路径配置
		 root 	/usr/share/nginx/index;# 根⽬录
		 index  index.html index.htm; # 默认⾸⻚
	 }
	 error_page 500 502 503 504 /50x.html; # 错误⻚⾯
	 location = /50x.html {
	 	root 	html;
	 }
	 }
	 server {
		 listen 82; # 监听的端⼝
		 server_name localhost; # 域名或ip
		 location / { # 访问路径配置
		 root /usr/share/nginx/regist;# 根⽬录
		 index regist.html; # 默认⾸⻚
	 }
	 error_page 500 502 503 504 /50x.html; # 错误⻚⾯
	 location = /50x.html {
		 root 	html;
	 }
 }

3、域名绑定

域名指向后,修改nginx配置⽂件 domain.conf

server {
	 listen 	 80;
	 server_name www.hisnxg.top;
	 location / {
		 root /usr/share/nginx/cart;
		 index cart.html;
	 }
 }
 server {
	 listen 	 80;
	 server_name regist.nxg.top;
	 location / {
		 root /usr/share/nginx/search;
		 index search.html;
	 }
 }

4、配置反向代理

~/nginx/config.d/proxy.conf

upstream tomcat-kkb{
		 server 139.244.55.130:8080;
	 }
 server {
	 listen		 80; # 监听的端⼝
	 server_name www.hisnxg.top; # 域名或ip
	 location / { # 访问路径配置
		 # root index;# 根⽬录
		 proxy_pass http://tomcat-kkb;
		 index index.html index.htm; # 默认⾸⻚
	 }
 }

5、配置负载均衡

修改 Nginx配置⽂件:lb.conf

upstream tomcat-huike {
		 server 139.244.55.130:8080;
		 server 139.244.55.130:8081 weight=2; # 权重,出现次数是其它服务器的2倍
		 server 139.244.55.130:8082;
	 }
 server {
	 listen		 80; # 监听的端⼝
	 server_name www.nxg.top; # 域名或ip
	 location / { # 访问路径配置
		 # root index;# 根⽬录
		 proxy_pass http://tomcat-huike;
		 index index.html index.htm; # 默认⾸⻚
	 }
	error_page 500 502 503 504 /50x.html; # 错误⻚⾯
	 location = /50x.html {
	 	root	 html;
	 }
 }

6、Nginx配置,解决跨域问题

server {
    listen 80;
    server_name localhost 127.0.0.1;
	location / {
		# 允许跨域请求的“域”
		add_header 'Access-Control-Allow-Origin' $http_origin;
		# 允许客户端提交Cookie
		add_header 'Access-Control-Allow-Credentials' 'true';
		# 允许客户端的请求方法
		add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
		# 允许客户端提交的的请求头
		add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
		# 允许客户端访问的响应头
		add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
		# 处理预检请求
		if ($request_method = 'OPTIONS') {
			# 预检请求缓存时间
			add_header 'Access-Control-Max-Age' 1728000;
			add_header 'Content-Type' 'text/plain; charset=utf-8';
			add_header 'Content-Length' 0;
			return 204;
		}
		
		# SpringBoot 应用访问路径
		proxy_pass http://127.0.0.1:8080;
		
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		
		proxy_connect_timeout 600;
		proxy_read_timeout 600;
	}
}

注:可以解决多数跨域问题

服务器在返回头信息里设置(必须):
Access-Control-Allow-Origin: Origin
Eg:Access-Control-Allow-Origin:127.0.0.1:80
如果想设置匹配所有的Origin且不带cookie的,可以设置:
Access-Control-Allow-Origin: *
如果需要带Cookie,需设置:
Access-Control-Allow-Credentials:true
如果想匹配所有的Origin且带cookie:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: 请求的Origin(从request获取后填入)
千万不能同时设置Credentials=true且Origin=,浏览器会报错: has been blocked by CORS
 policy: The value of the ‘Access-Control-Allow-Origin’ header in the
 response must not be the wildcard '’ when the request’s credentials
 mode is ‘include’. The credentials mode of requests initiated by the
 XMLHttpRequest is controlled by the withCredentials attribute