![Nginx - 服务器名称冲突问题和重定向循环](https://linux22.com/image/692946/Nginx%20-%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%90%8D%E7%A7%B0%E5%86%B2%E7%AA%81%E9%97%AE%E9%A2%98%E5%92%8C%E9%87%8D%E5%AE%9A%E5%90%91%E5%BE%AA%E7%8E%AF.png)
我只是想实现将对我的网站的任何请求(例如来自example.com
或www.example.com
或http://example.com
)重定向到https://www.example.com
。
但是,在当前配置下,我收到重定向循环和警告。
警告如下:
[警告] 0.0.0.0:443 上冲突的服务器名称“www.example.com”,已被忽略。
我的配置文件:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com;
}
server {
listen 443 ssl;
server_name example.com;
#redirect non www to www.
return 301 https://www.example.com;
# root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/140111/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/140111/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
#cache:
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
server {
listen 443 ssl;
server_name www.example.com;
root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/140111/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/140111/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
#cache:
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/after/*;
我尝试过的方法
我在 Google 上搜索了这个问题,找到了一些答案。我尝试过前往/etc/nginx/sites-available
并查找任何命名的文件default~
,但没有找到。
这段代码让我陷入无限循环。不知道为什么。
答案1
您可以使用此配置将所有 http 请求重定向到 https 并移动所有非 www 请求
我在我的服务器配置上使用它并且它们运行完美。
server {
listen 80;
server_name www.example.com example.com;
# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
return 302 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
include /etc/nginx/conf-available/ssl.conf;
# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
return 302 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
server_name www.example.com;
include /etc/nginx/conf-available/ssl.conf;
如果您正在使用 Wordpress,那么请不要忘记在常规设置中更改您的网站地址。
欲了解更多信息,请阅读此帖子。 如何在 Nginx 和 Apache 中将 wordpress 的非 www url 重定向到 www?
答案2
我发现问题出现是因为我在 sites-enabled 目录中有一个额外的文件。当我使用 nano 编辑时,它生成了一个 .save 文件...
因此尝试加载相同的文件两次..
答案3
您可以像这样对 HTTP/HTTPS 使用单个服务器块
server {
listen 80;
listen 443 default_server ssl;
server_name www.example.com;
ssl_certificate /path/to/my/cert;
ssl_certificate_key /path/to/my/key;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}