【发布时间】:2018-12-29 15:12:48
【问题描述】:
我正在尝试在我的 nginx 服务器上强制使用 www 和 https。
就像这里建议的其他问题一样,我实现了以下内容。
server_name example.com www.myexample.com
return 301 https://www.example.com$request_uri;
然后在下面我有以下内容是我们加密添加的:
if ($scheme != "https") {
return 301 "https://www.$host$request_uri";
}
当我访问该站点时,它转到 https://www,example.com,但我收到消息“www.example.com 重定向您的次数过多。”
如果我注释掉 Let's Encrypt 添加的位,我仍然会收到重定向消息。
只有当我注释掉以下内容时它才有效:
return 301 https://www.example.com$request_uri;
有人对如何设置有更好的想法吗?
我已经看到了另一个答案,但 OP 使用的是 cloudflare。我没有使用任何 CDN。
谢谢
这是我的所有信息。我的第一个 return 301 行被注释掉了,因为它导致了太多的重定向: NGINX:版本 1.10.3 Ubuntu:16.04.3 LTS(xenial)
# Default server configuration
#
server {
listen 80;
listen [::]:80;
client_max_body_size 25M;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/example.com/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
# return 301 https://www.example.com$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# hide user.ini file
location ~ ^/\.user\.ini {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
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
if ($scheme != "https") {
return 301 https://www.$host$request_uri;
} # managed by Certbot
}
【问题讨论】:
-
您好!请记住发布完整的虚拟主机配置,以便我们确定是否存在任何其他问题。此外,说明发行版名称、版本和 nginx 版本总是有帮助的,因为不推荐使用的功能和最佳实践发生了变化。