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

[求助]Openshift 的 nginx-php7 配置重定向 https 时出现循环重定向

  •  
  •   jmhjhjhj · 2017-01-30 16:00:57 +08:00 · 3215 次点击
    这是一个创建于 2636 天前的主题,其中的信息可能已经有所发展或是发生改变。
    可能用了假的 nginx , default.conf.erb 写法
    server {
    root <%= ENV['OPENSHIFT_REPO_DIR'] %>/www;
    listen <%= ENV['OPENSHIFT_PHP_IP'] %>:<%= ENV['OPENSHIFT_PHP_PORT'] %>;
    server_name <%= ENV['OPENSHIFT_APP_DNS'] %>;
    index index.php index.html index.htm <%= ENV['NGINX_EXTRA_INDEX'] %>;
    set_real_ip_from <%= ENV['OPENSHIFT_PHP_IP'] %>;
    real_ip_header X-Forwarded-For;

    # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    if ($server_port = 80){
    return 301 https://$server_name$request_uri;}
    if ($scheme = http){
    return 301 https://$server_name$request_uri;}

    # avoid caching by proxies
    add_header Cache-Control private;

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:<%= ENV['OPENSHIFT_PHP_DIR'] %>/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include openshift_params;

    # uncomment to export all environment variables to fastcgi
    #include <%= ENV['OPENSHIFT_REPO_DIR'] %>/config/nginx.d/export_env;
    }

    # avoid unnecessary log
    location = /favicon.ico {
    access_log off;
    log_not_found off;
    }

    location = /robots.txt {
    access_log off;
    log_not_found off;
    }

    # Handle any other URI
    location / {
    try_files $uri $uri/ =404;
    }
    }
    第 1 条附言  ·  2017-02-03 08:31:38 +08:00
    环境生成的 web 端口是 8080 ,再另加 server ssl 的话会 503 ,不管加不加 if 都会重定向循环
    4 条回复    2017-03-06 10:31:21 +08:00
    lhbc
        1
    lhbc  
       2017-01-30 17:01:04 +08:00 via iPhone
    不要用 if
    不要用 if
    不要用 if

    http 和 https 分开两段 server {}
    php7
        2
    php7  
       2017-02-01 10:34:37 +08:00 via Android
    提楼上的
    edyuy
        3
    edyuy  
       2017-02-24 22:22:18 +08:00   ❤️ 1
    OpenShift 的架构和一般的 VM 是不一样的,它的 https 由上一层的 nginx 所提供(所以你也发现了你的 nginx 配置里连证书和加密方式都不配置),所以在用户的应用层面拿到的都是 http 的请求(当然,也都是 80 端口的),其他答案那个方式也是不行的,因为你的应用并不会去监听 80 和 443 。正确的做法是,首先把你写的两段 if 替换为

    if ($http_x_forwarded_proto != "https") {
    rewrite ^(.*)$ https://$server_name$1 permanent;
    }

    使用上层 nginx 传来的客户端访问方式进行判断,其中 rewrite 改为 return 301 也是可以的
    jmhjhjhj
        4
    jmhjhjhj  
    OP
       2017-03-06 10:31:21 +08:00
    @edyuy 貌似并不好使,会提示 301 Moved Permanently ,然后循环就什么都没了,现在用 index 做 metahttp-equiv="refresh"勉强一下了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2811 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:23 · PVG 10:23 · LAX 19:23 · JFK 22:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.