nginx根據網域名稱做http,https分發

來源:互聯網
上載者:User

標籤:des   http   io   ar   os   sp   for   on   代碼   

omcat連接埠:8080 做好虛擬機器主機 參照我的另一篇文章
nginx連接埠:80 根據網域名稱指派

在conf/nginx.conf中的http中增加

include www.huozhe.com.conf

建立conf/www.huozhe.com.conf,內容如下:

server {
listen 80;
server_name www.huozhe.com;

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host:80;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Via "nginx";
}
}

其中127.0.0.1是你的tomcat主機ip

要是代理https的話,內容如下:

server {
listen 443;
server_name mail.huozhe.com;

ssl on;
ssl_certificate server.crt;
ssl_certificate_key server.key;

location / {
    proxy_pass https://192.168.0.2:443;
    proxy_set_header Host $host:443;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Via "nginx";

}
}

 

其中192.168.0.2是你的https主機
如果後端https沒有認證的話,可以如此簡化:

server {
listen 80;
server_name svn.huozhe.com;

location / {
    proxy_pass https://192.168.0.2:443;
    proxy_set_header Host $host:443;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Via "nginx";
    proxy_set_header X-Forwarded-Proto https; #注意看這裡 多了一行
}
}

 

如果提示“SSL 接收到一個超出最大准許長度的記錄。”  錯誤碼“ssl_error_rx_record_too_long”說明少了“ssl on;”這一行

後面的server.crt server.key是數位憑證,具體可以參照openssl做認證

openssl做認證================================================

mkdir ssl
cd ssl
openssl genrsa -des3 -out server.key 1024 # 會提示你輸入key,儘可能長些複雜些,後面好幾處要用,我都是複製粘貼的
openssl req -new -key server.key -out server.csr # 輸入組織資訊 CN BeiJing HaiDian huozhe.com
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

 

這裡的server.crt server.key你就可以拿去用了

nginx根據網域名稱做http,https分發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.