Spring Boot(十四)整合 Nginx

下载地址:http://nginx.org/en/download.html
上线的nginx.conf
Nginx 1.16.0

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #gzip  on;
    
    server {
        listen       443 ssl;
        server_name  www.你的域名.com;
		
		ssl_certificate 	../cert/2569073_www.你的域名.com.pem;   	#将domain name.pem替换成您证书的文件名。
		ssl_certificate_key ../cert/2569073_www.你的域名.com.key;     #将domain name.key替换成您证书的密钥文件名。
		
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #服务端要支持 协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
		#vue 网站目录				
        root D:\WebSite\dist;
		index index.html index.htm;
    }
    server {
        listen       443 ssl;
        server_name  api.你的域名.com;

		root   html;
		index  index.html index.htm;
		
        ssl_session_cache    	shared:SSL:1m;
		ssl_certificate   		../cert/2571250_api.你的域名.com.pem;
		ssl_certificate_key  	../cert/2571250_api.你的域名.com.key;
		ssl_session_timeout 5m;
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_prefer_server_ciphers on;
		 
        location / {
			proxy_set_header X-Real-IP $remote_addr;
			#proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			proxy_set_header X-Nginx-Proxt true;
			proxy_redirect off;
			proxy_set_header Cookie $http_cookie;
			proxy_set_header X-Forwarded-Host $host;
			proxy_set_header X-Forwarded-Server $host;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	   		
			add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS,PUT,DELETE';
			if ($request_method = 'OPTIONS') {
				add_header 'Access-Control-Allow-Origin' $http_origin;
				add_header 'Access-Control-Allow-Credentials' 'true';
				add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS,PUT,DELETE';
				add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept,jsessionid,cookie,set-cookie,token,Authorization';
				add_header 'Access-Control-Expose-Headers' 'Authorization';
				add_header 'Content-Type' 'application/json,charset=UTF-8,text/plain';
				return 200;
			}
			proxy_pass http://127.0.0.1:8090;
        }
    }
}

配置解析:
worker_processes 1;#需要与服务器核心数一致
配置文件部署了两个网站
第一个server:为vue站点,root 指定网站目录即可。
第二个server:为api服务,供vue,小程序 等调用。指向服务器的8090端。这里运行的是springboot的服务。
两个server 都是走https 443端口,证书均在阿里云上面申请的。

需要注意的是,Nginx 服务器的跨域访问,百度上很多资料说springboot做了跨域访问,Nginx就不用做跨域处理,然而并不是,还是得做跨域处理,而且最不能理解的是 add_header ‘Access-Control-Allow-Origin’ ‘’ always; header加上这个,就会提示指定域名,当改成 add_header ‘Access-Control-Allow-Origin’ ‘https://www.你的域名.com’; 这个时候虽然你的网站可以访问api了,但是很显然是不符合实际的,因为api是供多个域名或者ip访问的,不可能只指定一个域名,所以改成 add_header ‘Access-Control-Allow-Origin’ $http_origin; 所以请求域名都可以访问。不知道为什么不能用*,有空再折腾了。*

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的网友

day day up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值