# Nginx代理實現https頁面請求http


背景:前端使用ssl加密實現http,但是后端走的springboot的接口為了方便,就沒有配置ssl加密;

有很多種方案,如果時間多,最好的是前后端都用ssl加密證書實現https;

我這里使用一種最簡單的方式,使用nginx代理轉發的方式,把所有api的接口反向代理到http的域名上;

vue前端

所有請求接口為了方便nginx代理轉發;
baseURL:"前端的https域名/api"
例如:
baseURL: "https://test.******.com/api", 

nginx配置:包括ssl和反向代理(后端http域名)

server
{
    listen 80;
	 listen 443 ssl http2;
    server_name test..****..com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/te1..****..com;
    
    #SSL-START SSL相關配置,請勿刪除或修改下一行帶注釋的404規則
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/te1..****..com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/te1..****..com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;

    #SSL-END
    
    #ERROR-PAGE-START  錯誤頁配置,可以注釋、刪除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注釋或修改
    include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重寫規則引用,修改后將導致面板設置的偽靜態規則失效
    include /www/server/panel/vhost/rewrite/te1..****..com.conf;
    #REWRITE-END
    
       location /api {
        proxy_set_header  X-Real-IP   $remote_addr;
        # api_server是自己定義的upstream
        proxy_pass http://111.229.91.20:8081;
    }

    
    #禁止訪問的文件或目錄
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一鍵申請SSL證書驗證目錄相關設置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null; 
    }
    
    
    #添加攔截路徑和代理地址
    location /api/ {              
           proxy_pass http://111.****.91.20:8081/;  #注意:使用代理地址時末尾記得加上斜杠"/"。    
    }

    access_log  /www/wwwlogs/te1..****..com.log;
    error_log  /www/wwwlogs/te1..****..com.error.log;
}



我這里指出 反向代理部分:所有前端請求/api/的接口都會被反向代理轉發

    #添加攔截路徑和代理地址
    location /api/ {              
           proxy_pass http://后端接口域名:8081/;  #注意:使用代理地址時末尾記得加上斜杠"/"。    
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM