非 www 域名到 www 域名?

非 www 域名到 www 域名?

我已经更改了我的 conf 文件,以便当用户输入不带 www 的域时,它会重定向到带有 www 的域:

server_name example.com; 返回 301 $scheme://www.example.com$request_uri; 我还希望我的 https 适用于 /user 下的任何内容

我收到太多重定向的错误,我哪里错了?

所以我有:

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

最好的方法是将serverexample.com分开www.example.com。 的条目example.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;
                 }
 }

相关内容