Nginx阻止多个域将所有stream量redirect到https?

我有一台运行nginx 1.6的web服务器,其中包含一个IP地址,并托pipewww.domainname.com以及dev.domainname.com。

我试图find一个聪明的方式来路由所有的HTTPstream量到https,我想确保我的默认服务器是“www”现场版本的时间。 所以最终目标是,除非用户指定https://dev.domainname.com ,否则他们将被redirect到https://www.domainname.com 。

我的nginx.conf设置被configuration为包含'/ etc / nginx / etc / sites-enabled / *'。 所以我的configuration示例位于'etc / nginx / sites-enabled / www.domainname.com'。

另外,要澄清。 当您访问没有www的域名时,我当前的configuration有加载dev站点的问题。

编辑 :我只是在本地尝试这个方法,http和https不允许在'/ etc / nginx / sites-enabled /'中。 有没有更好的解决scheme,或者我应该把这个configuration到nginx.conf?

所以我的问题是有更好的方式来处理这种types的设置?

http { # all traffic should be over https listen 80 default; # listen for all server names server_name *.domainname.com; # redirect to www with https return 301 $scheme://www.domainname.com$request_uri; } https { # configuration for all https sites listen 443 default ssl; ssl on; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_page 404 /index.php; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } # configuration for the non-www redirect server { # non-www server name server_name domainname.com; # return to www return 301 $scheme://www.domainname.com$request_uri; } # configuration for the live website server { # www server name server_name www.domainname.com; # root to public directory root /path/to/www.domainname.com/public; # ssl certificates ssl_certificate /etc/nginx/ssl/www.domainname.com/ssl-bundle.crt; ssl_certificate_key /etc/nginx/ssl/www.domainname.com/server.key; # error logs for www site error_log /var/log/nginx/www.domainname.com-error.log error; } # configuration for the dev site server { # dev server name server_name dev.domainname.com; # root to public directory root /path/to/dev.domainname.com/public; # ssl certificates - using multi domain ssl ssl_certificate /etc/nginx/ssl/www.domainname.com/ssl-bundle.crt; ssl_certificate_key /etc/nginx/ssl/www.domainname.com/server.key; # error logs for dev site error_log /var/log/nginx/dev.domainname.com-error.log error; } } 

如果您的发行版是基于Debian的,则您已安装了sites-available/default文件。 这是为nginxconfiguration默认页面的文件。

您需要通过使用rm sites-enabled/default运行其符号链接来禁用此虚拟主机。

然后,您需要使用以下内容创build新的default

 server { listen 80 default_server; listen 443 default_server ssl; server_name _; ssl_certificate /path/to/your/certificate; ssl_certificate_key /path/to/your/key; redirect 301 https://www.domainname.com$request_uri; } 

此块确保所有请求到其他域未被列出的所有请求被redirect到https://www.domainname.com

然后,创build另一个文件,例如dev.domainname.com

 server { listen 443 ssl; server_name dev.domainname.com; ssl_certificate /path/to/your/certificate; ssl_certificate_key /path/to/your/key; # Other config for dev.domainname.com } 

这个块处理dev.domainname.com的请求。

最后, www.domainname.com

 server { listen 443 ssl; server_name www.domainname.com; ssl_certificate /path/to/your/certificate; ssl_certificate_key /path/to/your/key; # Other config for www.domainname.com } 

这个块处理www.domainname.com请求。

所有你需要的是这样的:

 server { listen 80; server_name *.domainname.com; return 301 https://$server_name$request_uri; }