Ubuntu Nginx将www域重定向到非www域

Ubuntu Nginx将www域重定向到非www域,ubuntu,nginx,redirect,certbot,Ubuntu,Nginx,Redirect,Certbot,我想去example.app搜索引擎优化,我遵循了一些教程和问题解决方案,但似乎无法让它工作 我添加了一个单独的服务器块,重定向到非www域,并多次重新启动nginx,同时也从一个匿名窗口访问我的站点 这是我的/etc/nginx/sites available/default 配置文件 任何帮助都将不胜感激 server { server_name www.example.app; return 301 $scheme://example.app$request_uri; }

我想去example.app搜索引擎优化,我遵循了一些教程和问题解决方案,但似乎无法让它工作

我添加了一个单独的服务器块,重定向到非www域,并多次重新启动nginx,同时也从一个匿名窗口访问我的站点

这是我的
/etc/nginx/sites available/default
配置文件

任何帮助都将不胜感激

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

server {
    listen 80;
    listen [::]:80;


    root /var/www/html/;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name exampe.app;

    location / {
        try_files $uri $uri/ =404;
    }
    
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 365d;
        }

        location ~*  \.(pdf)$ {
            expires 30d;
        }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
     deny all;
    }

    error_page 404 /customerror_404.html;
    location = /customerror_404.html {
        root /usr/shar/nginx/html;
        internal;
    }
    
}


server {
    root /var/www/html/;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name example.app; # managed by Certbot


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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.app/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.app/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 = example.app) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 ;
    listen [::]:80 ;
    server_name example.app;
    return 404; # managed by Certbot

}

我使用它从www域重定向到非www域

您需要告诉nginx您想从哪个主机重定向,在本例中是从www

如果这不起作用。您可能需要将服务器名称设置为如下所示

server_name www.example.app example.app;
因此,主机被识别

我使用它从www域重定向到非www域

您需要告诉nginx您想从哪个主机重定向,在本例中是从www

如果这不起作用。您可能需要将服务器名称设置为如下所示

server_name www.example.app example.app;

这样主机就可以被识别。

通过在末尾添加代码块来尝试。我还按照你的建议更改了
server\u name
,但运气不好。还是不行,只是出于好奇。您的服务器上的端口80打开了吗?是的,端口打开了。我将尝试复制您的场景并返回给您。通过在末尾添加代码块来尝试。我还按照你的建议更改了
server\u name
,但运气不好。还是不行,只是出于好奇。您的服务器上的端口80打开了吗?是的,端口打开了。我将尝试复制您的场景并返回给您。