如何全站启用 https 访问,按照网上教程来写配置的话网站会无限重定向。

2014-10-19 15:55:07 +08:00
 P013onEr
8966 次点击
所在节点    NGINX
11 条回复
bitwing
2014-10-19 16:20:36 +08:00
server {
listen 80;
server_name www.xxx.com xxx.com;
return 301 https://www.xxx.com$request_uri;
}
server {
listen 443;
server_name xxx.com;
return 301 https://www.xxx.com$request_uri;
}
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

server {
listen 443 default_server ssl;
server_name www.xxx.com;
keepalive_timeout 70;

ssl_certificate /etc/nginx/ssl/ssl-unified.crt;
ssl_certificate_key /etc/nginx/ssl/ssl2.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

這個配置都會重定向到 https://www.xxx.com
bitwing
2014-10-19 16:33:22 +08:00
額,發錯了,不是全站的,題主無視吧
ytf4425
2014-10-19 16:40:52 +08:00
把你的配置发来看看,我给你改
ytf4425
2014-10-19 17:09:29 +08:00
nginx的配置
你参考一下,改成自己的目录和域名
server
{
listen 80;
#listen [::]:80;
server_name www.gfw.im gfw.im;
rewrite ^/(.*) https://www.gfw.im/$1 permanent;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.gfw.im;

include wordpress.conf;
#error_page 404 /404.html;
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /home/wwwlogs/www.gfw.im.log access;
}

server
{
listen 443 ssl spdy;
ssl on;
ssl_certificate /home/wwwroot/ssl/ssl.crt;
ssl_certificate_key /home/wwwroot/ssl/ssl.key.out;
ssl_session_timeout 5m;
#listen [::]:80;
server_name www.gfw.im gfw.im;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.gfw.im;

include wordpress.conf;
#error_page 404 /404.html;
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /home/wwwlogs/www.gfw.im.log access;
}
YonionY
2014-10-19 18:02:36 +08:00
建议检查下程序有没有启用SSL的选项,有可能是你的程序设定了强制使用协议http,Nginx跳到https后程序只认http又跳转回http,nginx识别到http后又跳回https上,然后就死循环了,一些程序没有给出SSL选项就只能在源码中找到写死http的部分改成https来解决。
ghy459
2014-10-19 19:20:22 +08:00
我的配置,给你参考一下

server {

listen 80;
server_name hack0nair.me;

location / {
rewrite ^(.*)$ https://hack0nair.me$1 permanent;
}

}

# HTTPS server

server {

listen 443 ssl;
server_name hack0nair.me;

ssl_certificate /root/ssl/hack0nair.me.crt;
ssl_certificate_key /root/ssl/hack0nair.me.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!DSS:!PKS;
ssl_prefer_server_ciphers on;

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
# ssl_stapling on;
# ssl_stapling_verify on;

location / {
root /var/www/hack0nair.me;
index index.html;
}
}
P013onEr
2014-10-19 19:23:47 +08:00
@ytf4425 @YonionY 我用的是hexo。无限重定向nginx配置文件同一楼。
P013onEr
2014-10-19 23:21:26 +08:00
@ghy459 用了你的也是太多重定向。 = =。
typcn
2014-10-20 00:11:08 +08:00
find /path/to/hexo | grep "http:"

全改成 https
ProfFan
2014-10-20 01:03:27 +08:00
曾经遇到过,如果是多站点SSL跳的话记得要proxy_set_header Host $host; 否则有可能会因为后端应用的问题无限跳转。
P013onEr
2014-10-20 01:35:21 +08:00
搞定了。重新完整的弄了一下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/140009

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX