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

2017-01-30 16:00:57 +08:00
 jmhjhjhj
可能用了假的 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;
}
}
3227 次点击
所在节点    NGINX
4 条回复
lhbc
2017-01-30 17:01:04 +08:00
不要用 if
不要用 if
不要用 if

http 和 https 分开两段 server {}
php7
2017-02-01 10:34:37 +08:00
提楼上的
edyuy
2017-02-24 22:22:18 +08:00
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
2017-03-06 10:31:21 +08:00
@edyuy 貌似并不好使,会提示 301 Moved Permanently ,然后循环就什么都没了,现在用 index 做 metahttp-equiv="refresh"勉强一下了

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

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

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

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

© 2021 V2EX