nginx 重定向返回 301 代码而不是最终页面
nginx redirect returning 301 code instead final page
我使用 Bitnami Wordpress 创建了一个 EC2 实例,然后将该实例克隆到一个新实例中。
之后我在这两个实例中都修改了这个 NGINX 脚本以重定向 HTTP => HTTPS 请求。
# 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
登录,其中一个实例运行良好,return 最终页面源代码。
另一个实例是return这个:
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
考虑到它是一个克隆实例,这里会发生什么样的问题?
这里是 Bitnami 工程师:
X-Forwarded-Proto (XFP) header 是一个标准 header,用于识别客户端用于连接到您的代理或负载平衡器的协议(HTTP 或 HTTPS)。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
在这种情况下,当您直接连接到 NGINX 服务器时,您不需要使用那个 "if" 块,您可以简单地将您的请求重定向到 HTTPS
return 301 https://$host$request_uri;
您可以在此处找到有关 https 重定向的更多信息:
https://docs.bitnami.com/general/apps/wordpress-pro/administration/force-https-nginx/
我使用 Bitnami Wordpress 创建了一个 EC2 实例,然后将该实例克隆到一个新实例中。
之后我在这两个实例中都修改了这个 NGINX 脚本以重定向 HTTP => HTTPS 请求。
# 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
登录,其中一个实例运行良好,return 最终页面源代码。
另一个实例是return这个:
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
考虑到它是一个克隆实例,这里会发生什么样的问题?
这里是 Bitnami 工程师:
X-Forwarded-Proto (XFP) header 是一个标准 header,用于识别客户端用于连接到您的代理或负载平衡器的协议(HTTP 或 HTTPS)。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
在这种情况下,当您直接连接到 NGINX 服务器时,您不需要使用那个 "if" 块,您可以简单地将您的请求重定向到 HTTPS
return 301 https://$host$request_uri;
您可以在此处找到有关 https 重定向的更多信息:
https://docs.bitnami.com/general/apps/wordpress-pro/administration/force-https-nginx/