V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
P013onEr
V2EX  ›  NGINX

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

  •  
  •   P013onEr ·
    P013onEr · 2014-10-19 15:55:07 +08:00 · 8962 次点击
    这是一个创建于 3477 天前的主题,其中的信息可能已经有所发展或是发生改变。
    11 条回复    2014-10-20 01:35:21 +08:00
    bitwing
        1
    bitwing  
       2014-10-19 16:20:36 +08:00   ❤️ 1
    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
        2
    bitwing  
       2014-10-19 16:33:22 +08:00   ❤️ 1
    額,發錯了,不是全站的,題主無視吧
    ytf4425
        3
    ytf4425  
       2014-10-19 16:40:52 +08:00   ❤️ 1
    把你的配置发来看看,我给你改
    ytf4425
        4
    ytf4425  
       2014-10-19 17:09:29 +08:00   ❤️ 1
    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
        5
    YonionY  
       2014-10-19 18:02:36 +08:00   ❤️ 2
    建议检查下程序有没有启用SSL的选项,有可能是你的程序设定了强制使用协议http,Nginx跳到https后程序只认http又跳转回http,nginx识别到http后又跳回https上,然后就死循环了,一些程序没有给出SSL选项就只能在源码中找到写死http的部分改成https来解决。
    ghy459
        6
    ghy459  
       2014-10-19 19:20:22 +08:00   ❤️ 1
    我的配置,给你参考一下

    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
        7
    P013onEr  
    OP
       2014-10-19 19:23:47 +08:00
    @ytf4425 @YonionY 我用的是hexo。无限重定向nginx配置文件同一楼。
    P013onEr
        8
    P013onEr  
    OP
       2014-10-19 23:21:26 +08:00
    @ghy459 用了你的也是太多重定向。 = =。
    typcn
        9
    typcn  
       2014-10-20 00:11:08 +08:00   ❤️ 1
    find /path/to/hexo | grep "http:"

    全改成 https
    ProfFan
        10
    ProfFan  
       2014-10-20 01:03:27 +08:00   ❤️ 1
    曾经遇到过,如果是多站点SSL跳的话记得要proxy_set_header Host $host; 否则有可能会因为后端应用的问题无限跳转。
    P013onEr
        11
    P013onEr  
    OP
       2014-10-20 01:35:21 +08:00
    搞定了。重新完整的弄了一下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2828 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:22 · PVG 15:22 · LAX 00:22 · JFK 03:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.