nginx 将所有非 WWW 重定向到 HTTPS WWW
nginx redirect all non-WWW to HTTPS WWW
Here is what happens:
example.com -> https://example.com
http://example.com -> https://example.com
www.example.com -> -> https://www.example.com
This is what I want:
example.com -> https://www.example.com
http://example.com -> https://www.example.com
www.example.com -> -> https://www.example.com
我的 Nginx 配置中缺少什么?我无法从所有类似的问题中找出答案。一些答案说使用 "if" 语句,但 Nginx 文档特别指出不要这样做,因为它将适用于所有请求。
server {
listen 80;
listen 443; # add this line
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
root /var/www/html;
index index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
删除第二块 server_name
中的 example.com
并将 listen 443
添加到第二块。
希望对您有所帮助。
server {
listen 80;
listen 443; # add this line
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
listen 443; # add this line too.
root /var/www/html;
index index.php;
# Remove example.com
server_name www.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Here is what happens:
example.com -> https://example.com
http://example.com -> https://example.com
www.example.com -> -> https://www.example.com
This is what I want:
example.com -> https://www.example.com
http://example.com -> https://www.example.com
www.example.com -> -> https://www.example.com
我的 Nginx 配置中缺少什么?我无法从所有类似的问题中找出答案。一些答案说使用 "if" 语句,但 Nginx 文档特别指出不要这样做,因为它将适用于所有请求。
server {
listen 80;
listen 443; # add this line
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
root /var/www/html;
index index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
删除第二块 server_name
中的 example.com
并将 listen 443
添加到第二块。
希望对您有所帮助。
server {
listen 80;
listen 443; # add this line
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
listen 443; # add this line too.
root /var/www/html;
index index.php;
# Remove example.com
server_name www.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}