2015-10-05 94 views
0

我一直在尝试过去几天让这个工作正常,但我很快就来了。nginx强制SSL没有www重定向问题

(计算器不允许我发表两个以上的实测环节 - 忽略反斜杠)

通常的结果是,example\.netwww.example\.net会重定向到HTTP \ S://example.net,而http\s://www.example\.net将不会重定向到http \ s://example.net,而另一种常见情况是来自浏览器的声明该页面没有正确重定向的消息。

我想要做的是强制绝对所有请求去http \ s://example.net$request_uri。

  example\.net => http\s://example\.net 
     www.example\.net => http\s://example\.net 
https://www.example\.net => http\s://example\.net 

nginx的站点配置:

upstream backend { 
    server unix:/var/run/php5-fpm.sock; 
} 

server { 
    listen 80; 
    server_name www.example\.net example.net; 
    return 301 http\s://example\.net$request_uri; 
} 

server { 
    listen 443 ssl default_server; 

    server_name example\.net; 

    include snippets/snakeoil.conf; 

    root /var/www/html/example; 

    charset utf-8; 
    index index.php; 

    access_log /var/log/example-access.log; 
    error_log /var/log/example-error.log; 

    location/{ 
    try_files $uri $uri/ @extensionless-php; 
    index index.php; 
    } 

    location ~ .php { 
    try_files $uri =404; 
    fastcgi_pass backend; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include fastcgi_params; 
    } 

    location @extensionless-php { 
    rewrite ^(.*)$ $1.php last; 
    } 

    location /nginx_status { 
    stub_status on; 
    access_log off; 
    allow 127.0.0.1; 
    deny all; 
    } 
} 

谁能告诉我什么,我做错了什么?

回答

0

创建2个块来处理重定向到非www HTTPS。 最后一块是您放置常用网站配置/规则的地方。

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

server { 
    listen    443 ssl; 
    server_name   www.example.net; 
    ssl_certificate  path/to/cert; 
    ssl_certificate_key path/to/key; 
    return    301 https://example.net$request_uri; 
} 
server { 
    listen    443 ssl; 
    server_name   example.net; 
    ssl_certificate  path/to/cert; 
    ssl_certificate_key path/to/key; 

    # the rest goes here... 
}