Nginx阻止多个域将所有流量重定向到https?

时间:2014-06-30 01:08:29

标签: nginx configuration https

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

我正在尝试找到一种智能方法将所有http流量路由到https,我想确保我的默认服务器是当时的'www'实时版本。因此,最终目标是,除非用户指定https://dev.domainname.com,否则他们将被重定向到https://www.domainname.com

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

所以我的问题是有更好的方法来处理这种类型的设置吗?

# redirect all non https
server {
# 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;
}

# 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 {

# configuration for all https sites
listen 443 default_server ssl;
ssl on;

# 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;

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;
}

# 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;

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;
}

# error logs for dev site
error_log  /var/log/nginx/dev.domainname.com-error.log error;

}

0 个答案:

没有答案