我在Rackspace云上使用nginx,遵循一个教程,搜索了网络,到目前为止还不能得到这个排序。

我想要www.mysite.example去mysite。例如,正常的。htaccess为SEO和其他原因。

My /etc/nginx/sites-available/www.example.com.vhost config:

server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

我也试过

server {
       listen 80;
       server_name example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

我也试过。第二次尝试都给出重定向循环错误。

if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}

我的DNS设置为标准:

site.example 192.192.6.8 A type at 300 seconds
www.site.example 192.192.6.8 A type at 300 seconds

(示例ip和文件夹已用于示例和帮助人们在未来)。我使用Ubuntu 11。


当前回答

不确定是否有人注意到它可能是正确的返回301,但浏览器阻塞它做

rewrite ^(.*)$ https://yoursite.example$1;

比:

return 301 $scheme://yoursite.example$request_uri;

其他回答

我的配置是- Nginx + tomcat 9 + Ubuntu 20.04 + spring boot app 上面所有的答案都不适合我-也不适合Nginx文件中的上游符号-所以我改变了我的设置

感谢上帝的certbot -这个util非常有用,它为您的网站生成基本文件,然后我添加了我的更改-重定向https://www.example.com, http://www.example.com到只有一个https://example.com

server {
if ($host = www.example.com) {
    return 301 https://example.com$request_uri;
}
    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://127.0.0.1:8080; # This is upstream name, note the variable $scheme in it
      proxy_redirect off;
    }
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com;
    return 404; # managed by Certbot
}

试试这个

if ($host !~* ^www\.){
    rewrite ^(.*)$ https://www.yoursite.example$1;
}

其他方式: Nginx no-www to www

server {
  listen       80;
  server_name  yoursite.example;
  root /path/;
  index index.php;
  return       301 https://www.yoursite.example$request_uri;
}

WWW到no-www

server {
  listen       80;
  server_name  www.yoursite.example;
  root /path/;
  index index.php;
  return       301 https://yoursite.example$request_uri;
}

将non-www重定向到www

对于单一域:

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

适用于所有领域:

server {
        server_name "~^(?!www\.).*" ;
        return 301 $scheme://www.$host$request_uri;
}

将www重定向到非www 对于单一域:

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

适用于所有领域:

server {
         server_name "~^www\.(.*)$" ;
         return 301 $scheme://$1$request_uri ;
}

鬼的博客

为了使nginx推荐方法返回301 $scheme://example.com$request_uri;使用Ghost,你需要在你的主服务器块中添加:

proxy_set_header    X-Real-IP           $remote_addr;
proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
proxy_set_header    Host                $http_host;
proxy_set_header    X-Forwarded-Proto   $scheme;
proxy_set_header    X-NginX-Proxy       true;

proxy_pass_header   X-CSRF-TOKEN;
proxy_buffering     off;
proxy_redirect      off;  
if ($host ~* ^www.example.com$) {
    return 301 $scheme://example.com$request_uri;
}