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),反向代理转发到后台不同端口。
设置文件:
/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