Nginx:重定向mydomain.com到mydomain.es上DigitalOcean VPS

mbjcgjjk  于 2023-04-20  发布在  Nginx
关注(0)|答案(1)|浏览(109)

我有一个工作mydomain.com域已经在DigitalOcean VPS上工作,我使用Nginx提供服务。
现在我买了mydomain.es,并添加了DNS指向我的DigitalOcean VPS,我想它重定向到mydomain.com,我怎么能用nginx做到这一点?
这是我的nginx文件mydomain.com域名:

server {
    listen 80;
    server_name mydomain.com www.mydomain.com;

    root /var/www/mydomain.com/public;
    index index.php index.html;

    access_log /var/log/mydomain.com/access.log;
    error_log /var/log/mydomain.com/error.log;

    # serve static files directly
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
        log_not_found off;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
wkyowqbh

wkyowqbh1#

你真的只是想301重定向所有流量到现有的mydomain.com?
然后你可以简单地添加一个新的服务器块(通常在一个单独的配置文件中):

server {
    listen 80;
    server_name mydomain.es www.mydomain.es;
    return 301 $scheme://www.mydomain.com$request_uri;
}

注意:这只是将非SSL流量重定向到标准端口80。通常您会为端口443的SSL流量添加一个单独的服务器块。

相关问题