首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx重定向返回301代码,而不是最终页面

nginx重定向返回301代码,而不是最终页面
EN

Stack Overflow用户
提问于 2019-02-05 00:15:21
回答 1查看 277关注 0票数 0

我使用Bitnami Wordpress创建了一个EC2实例,然后将该实例克隆到一个新实例中。

之后,我在两个实例中都修改了这个NGINX脚本,以重定向HTTPS请求。

代码语言:javascript
复制
# HTTP server

server {
    listen       80;
    server_name  localhost;

    #include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";

    if ($http_x_forwarded_proto = 'http'){
            return 301 https://$host$request_uri;
    }

    include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}

# HTTPS server

server {
   listen       443 ssl;
   server_name  localhost;

   ssl_certificate      server.crt;
   ssl_certificate_key  server.key;

   ssl_session_cache    shared:SSL:1m;
   ssl_session_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;
   ssl_prefer_server_ciphers  on;

   #include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";

   include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}


include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-vhosts.conf";

通过SSH登录并运行curl -i localhost,其中一个实例运行良好,并返回最终页面源代码。

另一个实例返回以下内容:

代码语言:javascript
复制
bitnami@ip-xxx-xx-xx-xxx:~$ curl -i localhost
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Mon, 04 Feb 2019 16:05:51 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.1.22
Location: https://localhost/
X-Frame-Options: SAMEORIGIN

考虑到它是一个克隆实例,这里会发生什么样的问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-05 16:16:21

Bitnami工程师:

X-Forwarded-Proto (XFP)标头是一个标准标头,用于标识客户端用于连接到代理或负载均衡器的协议(HTTP或HTTPS)。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto

在这种情况下,由于直接连接到NGINX服务器,因此不需要使用"if“块,只需将请求重定向到HTTPS即可

代码语言:javascript
复制
      return 301 https://$host$request_uri;

您可以在此处找到有关https重定向的更多信息:

https://docs.bitnami.com/general/apps/wordpress-pro/administration/force-https-nginx/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54520120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档