如何将www重定向到非www nginx

628mspwn  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(120)

我试图将www请愿重定向到非www域,我尝试了每个人都说的建议,但对我不起作用,这是我的nginx配置文件:

server {
    listen 80;
    server_name www.site.com;
    return 301 $scheme://site.com$request_uri;
}
server {
            listen 80;
            server_name _;
            return 301 https://$host$request_uri;
}
server {
        listen 443 ssl;

        root /var/www/site.com/site;
        index index.html index.htm index.nginx-debian.html;

        server_name site.com;
        ssl_certificate /etc/nginx/ssl/site.com/site.>
        ssl_certificate_key /etc/nginx/ssl/site.com/myserver.key;
        ssl_prefer_server_ciphers on;

        location / {
                try_files $uri $uri/ =404;
        }
}
3mpgtkmj

3mpgtkmj1#

这个配置适合我:

server {
  listen 80;
  listen [::]:80;
  server_name  www.example.com;
  rewrite ^ http://example.com/ permanent;

  listen 443 ssl;
  listen [::]:443 ssl;
  ssl on;
  ssl_certificate /etc/nginx/sslcert/server/server.pem;
  ssl_certificate_key /etc/nginx/sslcert/server/server.key;
  server_name  www.example.com;
  rewrite ^ https://example.com/ permanent;             
}

我已经添加了http协议沿着nginx配置,我还没有在我自己的服务器上使用。但是,此配置应该适用于您的情况。

相关问题