Nginx搭建HTTPS【前後端分離項目】服務器

希望能解決你的問題

你好!請看
我的環境:VUE + SpringBoot
前後端代碼部署在一臺服務器上面。

[root@tt ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module

注意安裝Nginx時,一點要安裝ssl_module,否則Nginx的配置會報錯,找不到ssl_certificate命令

話不多說,直接上配置


#user  nobody;
worker_processes  1;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.baidu.com; #填寫你的服務器域名
		# 將所有http請求轉發到https
		rewrite ^(.*)$ https://$host$1 permanent;
	
        location / {
	   		root   html; 
	   		index  index.html index.html;   
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
 		}
    }
    # HTTPS server
    server {
	listen 443 ssl; # 新版Nginx需要在此處開啓ssl,老版本使用ssl on
	server_name www.baidu.com;  # localhost修改爲您證書綁定的域名。

	ssl_certificate baidu.com.pem;   #將domain name.pem替換成您證書的文件名。
	ssl_certificate_key baidu.com.key ;   #將domain name.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 / {
          root   /usr/demo/web/dist/;
	   	  index  index.html index.htm;
        }
	# 將所有prod-api的地址代理到接口服務器上	
	location /prod-api{
		   #此處需要特別注意:
		   #1、後端接口的端口和前端端口不一致,會出現跨域情況,
		   #2、所有/prod-api請求都會被轉發到http://www.baidu.com:9999/prod-api,在服務器內部進行
           proxy_pass http://www.baidu.com:9999/prod-api; # 將地址代理到api上
        }
    }
}

如果你發現錯誤,請指出,如果你覺得有幫助,請點贊!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章