在主域中应用SSL和www,而不是在nginx中的子域中应用

时间:2017-01-02 12:28:01

标签: ssl nginx

每当我为域名输入任何内容(“xyz.com”)时,我正在浏览器中将SSL(https)和www应用于域名(“https:// www.xyz.com”)。

我也有子域名,我不想在子域名中应用“www”(这很好)。

除非我在浏览器中键入“https:// xyz.com”,否则一切都很有效,ngingx不应用www(它应该是“https:// www.xyz.com”)但是它给出了“ https:// xyz.com“仅限。

以下是sites-available:

的配置文件
server {
   listen 443;
   server_name xyz.com *.xyz.com;
   ssl on;
   ssl_certificate /etc/nginx/ssl/xyz.crt;
   ssl_certificate_key /etc/nginx/ssl/*.xyz.com.key;
   ssl_session_timeout 5m;
   ssl_protocols SSLv3 TLSv1;
   ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
   ssl_prefer_server_ciphers on;
   location / {
       proxy_pass http://127.0.0.1:8069;
       proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
       proxy_buffer_size 128k;
       proxy_buffers 16 64k;
       proxy_redirect off;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       location ~* /web/static/ {
           proxy_buffering off;
           proxy_pass http://127.0.0.1:8069;
       }
   }
}

server {
    listen 80;
    server_name xyz.com;
    add_header Strict-Transport-Security max-age=2592000;
    rewrite ^/.*$ https://www.$host$request_uri? permanent;
}

server {
    listen 80;
    server_name *.xyz.com;
    add_header Strict-Transport-Security max-age=2592000;
    rewrite ^/.*$ https://$host$request_uri? permanent;
}

请指导我在哪里做错了。

提前致谢。

1 个答案:

答案 0 :(得分:1)

add_header Strict-Transport-Security max-age=2592000;选项移至带有443端口的ssl服务器块。

server {
  listen 443;
  add_header Strict-Transport-Security max-age=2592000;
  # rest configs
}

将http 80块更改为

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://www.example.com$request_uri;  # permenent redirect
}

注意:对nginx配置所做的任何更改都需要向{nginx进程发送reload信号,以便在ubuntu中应用所需的更改sudo service nginx reload