HTTPS www到NGINX上的非www

时间:2014-12-15 10:13:33

标签: ssl nginx https no-www

我遇到的问题仅从https / ssl重定向到www到非www。任何想法为什么会这样?它适用于非安全URL。这是我的配置:

html {
    # Compression

    # Enable Gzip compressed.
    gzip on;

    # Enable compression both for HTTP/1.0 and HTTP/1.1.
    gzip_http_version 1.1;

    # Compression level (1-9).
    # 5 is a perfect compromise between size and cpu usage, offering about
    # 75% reduction for most ascii files (almost identical to level 9).
    gzip_comp_level 5;

    # Don't compress anything that's already small and unlikely to shrink much
    # if at all (the default is 20 bytes, which is bad as that usually leads to
    # larger files after gzipping).
    gzip_min_length 128;

    # Compress data even for clients that are connecting to us via proxies,
    # identified by the "Via" header (required for CloudFront).
    gzip_proxied any;

    # Tell proxies to cache both the gzipped and regular version of a resource
    # whenever the client's Accept-Encoding capabilities header varies;
    # Avoids the issue where a non-gzip capable client (which is extremely rare
    # today) would display gibberish if their proxy gave them the gzipped version.
    gzip_vary on;

    # Compress all output labeled with one of the following MIME-types.
    gzip_types application/atom+xml application/x-javascript application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component;
}

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

server {
    listen *:443 ssl;
    server_name www.example.com;
    return https://example.com$request_uri 301;
}

server {
    listen 443 ssl;
    server_name example.com;
    root /home/forge/default/public;

    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_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_param DB_PASSWORD "password";
        fastcgi_param DB_USERNAME "user";
        fastcgi_param DB_NAME "db";
        fastcgi_param DB_HOST "localhost";
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    # Expire rules for static content

    # cache.appcache, your document html and data
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    # Feed
    location ~* \.(?:rss|atom)$ {
      expires 1h;
      add_header Cache-Control "public";
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }
}

2 个答案:

答案 0 :(得分:0)

nginx从'default_server'获取SSL证书,或从首次描述的特定IP +端口对获取SSL证书。

所以,你应该

  • 交换启用SSL的server
  • 或者只需在第二个区块中将default_server参数添加到listen指令。

答案 1 :(得分:0)

好的,所以我刚才想出来了,但是忘记发布答案,所以就是这样。

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

server {
    listen 443 ssl;
    server_name example.com;
    root /home/forge/default/public;

    if ($host = 'www.example.com') {
        rewrite ^/(.*)$ https://example.com/$1 permanent;
    }
}

这是多么强大我不完全确定但它有效。

基本上,您在端口80上收听www和非www,并将301重定向返回到安全的非www URL。然后,如果已请求的主机与安全URL的www版本匹配,则检入端口443上的SSL服务器块,如果是,则将其永久重写为安全的非www版本。