用于HTTPS的Nginx配置(WWW到非WWW以及所有HTTP到HTTPS)

时间:2016-01-18 09:51:55

标签: nginx https configuration config

我无法获取我的HTTP - > HTTPS规则在我的NGINX配置中正常工作。除了HTTPS之外,我还尝试将WWW重定向到NON-WWW。但是,我无法让WWW工作。

我在这里:

http://example.com - > https://example.com(工作)

http://example.com?id=3 - > https://example.com?id=3(与QUERY STRING一起工作)

http://www.example.com - > https://example.com(DOESNT WORK)

https://www.example.com(不工作)

带有WWW的URL显示“找不到服务器。Firefox无法在www.example.com找到服务器。”

server {
    listen 80;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
    # this doesn't work. trying to http://www.example.com --> https://example.com
}
server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;

    # this works. http://example.com -> https://example.com
}
server {
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/sslmate/www.example.com.chained.crt;
    ssl_certificate_key /etc/sslmate/www.example.com.key;

    server_name www.example.com;
    return 301 https://example.com$request_uri;

    # this has www in server_name, and doesn't work
}
server {
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/sslmate/www.example.com.chained.crt;
    ssl_certificate_key /etc/sslmate/www.example.com.key;

    server_name example.com;

    root /home/garrett/domains/example.com/public_html;
    access_log /home/garrett/domains/example.com/logs/access.log;
    error_log /home/garrett/domains/example.com/logs/error.log;

    index index.php index.html index.htm;
    error_page 404 /404.php;
    error_page 403 /404.php;

    location / {
        #try_files $uri $uri/ /index.php;
    }

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm-garrett.sock;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    # this block works, because I can access https://example.com correctly
}

我做错了什么?

0 个答案:

没有答案