非www到www域名?

非www到www域名?

我更改了我的 conf 文件,以便當用戶在不帶 www 的網域中鍵入時,它會重定向到帶有 www 的網域:

伺服器名稱 example.com;返回 301 $scheme://www.example.com$request_uri;我還希望 /user 下的任何內容都使用我的 https

我收到了“太可能重定向”的錯誤,我哪裡出錯了?

所以我有:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/example.com/site;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
}
location /user {
        rewrite ^ https://$http_host$request_uri? permanent;
}
}

對於連接埠 443:

server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

root /var/www/example.com/site;
index index.html index.htm;

ssl on;
ssl_certificate //path here
ssl_certificate_key //path here

location / {
        rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}

答案1

做到這一點的最佳方法是為和建立單獨的server區塊。應該的條目example.comwww.example.comexample.com僅有的有:

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

的條目www.example.com將包含其他所有內容(顯然不是有重定向)。

同樣,您將有兩個單獨的 https 伺服器項目(連接埠 443)。

答案2

我會做這樣的事情:(未經測試)

server {
listen       80;
server_name  example.com;
return       301 http://www.example.com$request_uri;
}
server {
       listen       80;
       server_name  www.example.com;
       root /var/www/example.com/site;
       index index.html index.htm;

location / {
       rewrite ^/index.php;
          }
}
server {
   listen 443;
   server_name www.example.com;
   root /var/www/example.com/site;
   index index.html index.htm;

   ssl on;
   ssl_certificate //path here
   ssl_certificate_key //path here


  location /user {
                  rewrite ^ https://$http_host$request_uri? permanent;
                 }
 }

相關內容