nginx 一切-301 到子域名

nginx 一切-301 到子域名

我在尝试将所有内容重定向到 nginx 上的子域时遇到了一些麻烦。我正在使用 cloudflare 运行子域,这就是为什么我需要将流量重定向到此子域。

*.soldadinhos.global->站点.soldadinhos.global

soldadinhos.global -> 网站.soldadinhos.global

我该如何做呢?

server {
    server_name site.soldadinhos.global;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    root /usr/share/nginx/html;
    include fastcgi.conf;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/soldadinhos.global/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/soldadinhos.global/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 {
   server_name *.soldadinhos.global soldadinhos.global;
   listen 80;
   return 301 https://site.soldadinhos.global;
}

编辑:我已经修改了 conf 文件,它现在可以工作了,但如果我访问 www.soldadinhos.global/somefile.php,它不会重定向到 site.soldadinhos.global/somefile.php

答案1

您可以执行以下操作:

server {
    server_name *.example.com example.com;

    listen 80;

    return 301 http://site.example.com$request_uri;
}

您需要example.com从现有配置中删除。

然后您需要向您的 DNS 添加指向该服务器的相应 DNS A 记录。

相关内容