【问题标题】:Nginx http redirect to https doesn't workNginx http重定向到https不起作用
【发布时间】:2017-09-20 06:39:21
【问题描述】:

我正在为我的石墨网络使用 nginx 反向代理。 我的 nginx.conf 看起来有点像这样

server {
           listen 8081;
           server_name myserver.com;
           return 301 https://$host:5000$request_uri;
       }

 server {
       listen 5000 ssl;
       server_name myserver.com;

       location / {
           proxy_pass                 http://127.0.0.1:8080;
           proxy_set_header           X-Real-IP   $remote_addr;
           proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
           proxy_set_header           X-Forwarded-Proto  $scheme;
           proxy_set_header           X-Forwarded-Server  $host;
           proxy_set_header           X-Forwarded-Host  $host;
           proxy_set_header           Host  $host;


           add_header 'Access-Control-Allow-Methods' 'GET, POST';
           add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
           add_header 'Access-Control-Allow-Credentials' 'true';

           # here comes the basic auth, after the options part
           auth_basic            'Restricted';
           auth_basic_user_file  path/to/.htpasswd;

       }

       ssl    on;
       ssl_certificate    path/to/crt;
       ssl_certificate_key    path/to/key;
   }

我开始在https://host:5000 上使用我的网站,并且我希望将对该网站的任何 http 请求重定向为 https 请求。截至目前,我看到它被重定向到http://host/

这只发生在登录和注销期间。

我看到很多例子,他们使用端口 80 来监听 http 流量,但我不能使用该端口,因为其他一些应用程序已经在使用该端口。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您是否尝试过将 80 更改为您的服务器在 nginx 服务器设置中侦听的任何端口?
  • 我在 conf 文件中进行了我需要的所有更改。这里我为http流量指定了8081端口。
  • 8081 上运行的是什么?那是没有收到转发的请求?
  • 8081上没有运行任何东西。它的目的是将port5000上的http流量重定向为https

标签: nginx graphite


【解决方案1】:

尝试使用$server_name 代替$host,例如:

server {
   listen 8081;
   server_name myserver.com;
   return 301 https://$server_name:5000$request_uri;
}

您确实可以将您的域直接放在重定向上,例如:

return 301 https://myserver.com:5000$request_uri;

这可能不是你的情况,但由于你使用不同的端口可能值得检查error_page 497,这主要用于处理已发送到 HTTPS 端口的普通 HTTP 请求:

server {
  listen      8081 ssl;
  server_name your.site.tld;
  ssl         on;
  ...
  error_page  497 https://$host:5000$request_uri;
  ...
}

要了解有关497 的更多信息,请查看http://nginx.org/en/docs/http/ngx_http_ssl_module.html 中的“错误处理”部分

【讨论】:

  • 好吧,使用 error_page 似乎对我有用。不再需要另一个 8081 端口。谢谢
猜你喜欢
  • 2022-01-14
  • 2014-08-14
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 2018-06-11
  • 2015-07-28
  • 1970-01-01
相关资源
最近更新 更多