在nginx配置部分https

來源:互聯網
上載者:User

在昨天的文章(配置https實現請求安全)裡,我介紹了nginx配置全站的https訪問,這樣對所有請求都會加密,會減慢響應速度。

在實際的項目中,我們也沒有必要對所有請求加密,比如瀏覽一個貼文清單,查看一個商品資訊等等,只需要對一些包含敏感性資料的請求加密就可以了。

因此,今天嘗試配置了nginx部分的https訪問,這裡的測試會繼續使用到昨天產生的秘鑰和認證,對登陸頁面進行加密,配置的代碼如下:

upstream site{           server localhost:8081;           server localhost:8082;           server localhost:8083;        }        server        {                listen       9001;                server_name  localhost;                index index.jsp index.html;                root /home/default;                location ~ .* {                        proxy_pass http://site;                        proxy_set_header X-Real-IP $remote_addr;                }                location ^~ /test/login.jsp{                        rewrite ^ https://$server_addr:443$request_uri? permanent;                }        }        server        {                listen 443 ssl;                server_name localhost;                ssl on;                ssl_certificate /home/dayuanzi/temp/rsamiyao.csr;                ssl_certificate_key /home/dayuanzi/temp/rsamiyaonopw.key;                location / {                        rewrite ^ http://$server_addr:9001$request_uri? permanent;                }                location ^~ /test/login.jsp{                        proxy_pass http://127.0.0.1:8081;                        proxy_redirect off;                        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;                }        }

部署伺服器詳情:

三台tomcat:

localhost:8081,localhost:8082,localhost:8083

一台nginx


在上面的配置實現了以下功能:

1、nginx作為一個負載平衡器,除了login.jsp請求,其他請求都會平均分發到其他三個本機伺服器

2、http://localhost:9001/test/login.jsp時,會重新導向為https://localhost/test/login.jsp,對login.jsp加密

3、https://localhost/test/* 時,會重新導向為http://locathost:9001/* ,一般請求不加密

這裡簡單說一下rewrite文法:rewrite regex replacement [flag];

flag可以有四個取值:
    last : 相當於Apache的[L]標記,表示完成rewrite
    break : 停止執行當前虛擬機器主機的後續rewrite指令集
    redirect : 返回302臨時重新導向,地址欄會顯示跳轉後的地址
    permanent : 返回301永久重新導向,地址欄會顯示跳轉後的地址

另外,測試時,如果是在另一台機器訪問,上面rewrite ^ http://$server_addr:9001$request_uri?中,必須要用$server_addr不能用$server_name,否則會重新導向到本機


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.