如何让 nginx 通过 server_name 正则表达式返回接受参数?

如何让 nginx 通过 server_name 正则表达式返回接受参数?

我想在 ubuntu nginx 服务器上托管几个网站,提供来自 的内容/dirs/com.website。我的配置有什么问题?curl -Ihttp://domain.tld重定向到301 https://www..tld而不是https://www.domain.tld 请帮忙 :) 我迷路了

这是我的网站配置文件:

server {
    listen                   443 ssl http2 default_server;
    listen              [::]:443 ssl http2 default_server;
    ssl_certificate     /etc/letsencrypt/live/website.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/website.tld/privkey.pem;
    ssl_dhparam         /etc/nginx/ssl/dhparam.pem;
    index               index.html index.htm index.php index.nginx-debian.html;
    server_name         ~^www\.(?<domain>\w+)\.([(com)(co\.uk)(uk)])$;
    root                /dirs/www/com.$domain;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}
server {
    listen                   80;
    listen              [::]:80;
    server_name         ~^(www\.)?(?<domain>\w+)\.(?<tld>[(com)(co\.uk)(uk)])$;
    return              301 https://www.$domain.$tld$request_uri;
}
server {
    listen                   443 ssl http2;
    listen              [::]:443 ssl http2;
    ssl_certificate     /etc/letsencrypt/live/website.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/website.tld/privkey.pem;
    ssl_dhparam         /etc/nginx/ssl/dhparam.pem;
    server_name         ~^(?<domain>\w+)\.(?<tld>[(com)(co\.uk)(uk)])$;
    return              301 https://www.$domain.$tld$request_uri;
}

答案1

你把事情搞得太复杂了,domain.tld其实很容易访问,无论是通过https://nginx.org/r/$server_name还是https://nginx.org/r/$host取决于你是否想使用客户端指定的主机或 NGINX 匹配的主机。

相关内容