標籤:連接埠 permanent list 環境 real-ip dia request rewrite 地方
version: 1.1(fixed) 修正一些錯誤
基本環境:
/etc/nginx/nginx.conf #保持
/etc/nginx/ssl/ #ssl認證檔案
/etc/nginx/site-available #VirtualHost設定區
/etc/nginx/site-enable #VirtualHost應用區
基本設計:
讓nginx支援多網域名稱(包括次層網域),並自動跳轉到https(所有網域名稱預設訪問https),反向 Proxy轉寄到後台不同連接埠。
設定檔案:
/etc/nginx/site-available/virtualhost1.conf
upstream vs1{
#這裡可以設定負載平衡
server 127.0.0.1:3000;
}
server {
listen 80 ;
server_name www.youdomain.com;
#這裡設定跳轉,return 301不太好用~~
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
#設定ssl認證檔案
ssl_certificate /etc/nginx/ssl/startssl_ca.crt;
ssl_certificate_key /etc/nginx/ssl/startssl_ca.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name youdomain.com www.youdomain.com;
#這裡設定網域名稱跳轉名稱不變
server_name_in_redirect off;
error_page 502 /errors/502.html;
# 這裡設定預設https跳轉
error_page 497 https://$host$uri?$args;
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
access_log off;
expires max;
}
location /errors {
internal;
}
location / {
proxy_redirect off;
proxy_pass http://vs1;
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;
proxy_set_header Host $host;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
}
}
/etc/nginx/site-available/virtualhost2.conf
#與上一個檔案同樣的地方略去。
upstream vs2{
#這裡連接埠為4000,可以設定負載平衡
server 127.0.0.1:4000;
}
server {
listen 80;
server_name subdomain.yourdomain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
#可以設定獨立的ssl認證
ssl_certificate /etc/ssl/nginx/startssl_1_ca.crt;
ssl_certificate_key /etc/ssl/nginx/startssl_1_ca.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
#這裡設定了次層網域跳轉,需要在DNS處設定一條A記錄,也可以支援其它頂級網域名稱
server_name vs2.youdomain.com;
server_name_in_redirect off;
error_page 502 /errors/502.html;
error_page 497 https://$host$uri?$args;
# ....
location / {
proxy_redirect off;
proxy_pass http://vs2;
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;
proxy_set_header Host $host:server_port;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
}
#....
}
設定應用:
在/etc/nginx/site-enable下做串連
$sudo ln -s vs1.conf /etc/nginx/site-available/vs1.conf
測試組態檔案是否正確
$sudo nginx -t
讓nginx重新載入設定:
$sudo nginx -s reload
測試通過環境:ubuntu 13-server
參考:
nginx 的多網域名稱多https轉寄設定方法-million-ChinaUnix部落格
http://blog.chinaunix.net/uid-231372-id-4584714.html
nginx 的多網域名稱多https轉寄設定方法【轉】