nginx监听443端口 后面是apache监听8443, apache 返回301, nginx 如何正确处理?

nginx监听443,apache监听8443。

nginx配置

server {
  listen 443;
  # server_name *.ht920.com ht920.com;
  server_name www.ht920.com;
  ssl on;
  ssl_certificate   cert/1523974750873.pem;
  ssl_certificate_key  cert/1523974750873.key;
  ssl_session_timeout 5m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  location / {
    index index.html index.htm index.php;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass https://localhost:8443;
  }
  access_log logs/ht920.com_access.log;
}

现象是:
我访问 https://www.ht920.com/e/admin/ 正常访问,
访问 https://www.ht920.com/e/admin 不行,会跳转到 https://www.ht920.com:8443/e/admin/

我在服务器访问了curl -k https://localhost:8443/e/admin
得到结果是:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://localhost:8443/e/admin/">here</a>.</p>
</body></html>

问题是,我要如何配置,才能正确301跳转?

阅读 4.2k
2 个回答

可以在server_name下面这行这个试试

server {
  listen 443;
  # server_name *.ht920.com ht920.com;
  server_name www.ht920.com;
  server_name_in_redirect off;
  ssl on;
  ssl_certificate   cert/1523974750873.pem;
  ssl_certificate_key  cert/1523974750873.key;
  ssl_session_timeout 5m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  location / {
    index index.html index.htm index.php;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass https://localhost:8443;
  }
  access_log logs/ht920.com_access.log;
}

应该用proxy_redirect

proxy_redirect https://www.ht920.com:8443/ /;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题