ngnix www to no-www on certbot 生成的配置文件



我正在尝试获取对 example.com 的请求以及 www.example.com 在下面显示的配置文件中转到 https://example.com。该文件与 certbot 生成的完全相同。

将两个return 301语句更改为

return 301 https://example.com$request_uri;

不起作用,因为 https://www.example.com 仍然会去 https://www.example.com 而不是想要的 https://example.com

如果有人能指出获得所需结果所需的确切更改,将不胜感激。简化的说明将是一个奖励,因为我对nginx和certbot都很陌生。谢谢。

server {
root /var/www/html/drupal;
index  index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri /index.php?$query_string;        
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ [^/].php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/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 = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}

打开括号以获得更清晰的方式。

创建 2 个,而不是一个 443 侦听器。与 80 个相同。

这样,您就更容易知道什么在做什么,每对主机和架构都有一个配置。

server {
listen 80;
listen [::]:80;
server_name  www.example.com; #this will only listen to http://www.example.com
location / {        
return 301 https://example.com$request_uri; #and will upgrade to https
}
#we don't want that many redirects, so this will go directly to example.com
}
server {
listen 80;
listen [::]:80;
server_name  example.com; #this will only listen to http://example.com
location / {        
return 301 https://$host$request_uri; #and will upgrade to https
}
}
server {
server_name  www.example.com;
location / {
return 301 https://example.com$request_uri #this redirects to non-www
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
#same server configuration as your first server bracket, only accepting     https://example.com and not www.
}

我看到您正在将到达的连接发送到Drupal,因此认为Drupal有一个变量$base_url,它所做的任何重定向都是对该主机进行的,因此如果将其设置为 www.example.com,则nginx conf无关紧要,因为Drupal本身也可以进行重定向。

希望它有帮助,对任何问题发表评论。

它现在可以工作了,@flaixman。我从您的建议中做了一个更改 - 即只为 80 个块制作一个块,因为它们都做了完全相同的事情。所以,这是最终版本:(我希望以后不会搞砸一些可能导致问题的东西。

server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location / {
return 301 https://example.com$request_uri;
}
}
server {
server_name www.example.com;
location / {
return 301 https://example.com$request_uri;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/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{
root /var/www/html/d8;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri /index.php?$query_string;        
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ [^/].php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/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
}

最新更新