http - nginx 将所有非 WWW 重定向到 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;
}
}
解决方案
删除第二example.com
个server_name
块并添加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;
}
}
推荐阅读
- r - R:合并两个数据帧而不增加行
- linux - 获取 [: !=: shell 脚本中预期的一元运算符
- mysql - 如果未找到特定记录,则 MySQL 查询获取默认记录
- python - 如何用另一列中的每个值减去列值(熊猫)
- excel - 为什么 Range.Range 不能按照 MS 文档中的说明工作
- php - 当 WooCommerce 订单状态待付款时停止发送“处理订单”电子邮件
- python - Python:创建自定义 pd.dataframe 类
- reactjs - 在 useEffect 中反应钩子 updateState
- vue.js - VueJS 尝试使用 2 个不同的变量从 html 访问数据
- python - 在 Django 中创建自定义用户时如何检查是否生成了 uuid 字段?