【问题标题】:Nginx: How to redirect each http:port requests to HTTPS:port in the below config?Nginx:如何在以下配置中将每个 http:port 请求重定向到 HTTPS:port?
【发布时间】:2020-07-06 07:40:38
【问题描述】:

这是我的 nginx.conf,适用于 https。

如果有人键入 HTTP://dev.local.org:3002,我如何重定向到 HTTPS://dev.local.org:3002?

这个 nginx 在一个 docker-compose 容器中。

worker_processes 1;
events {
    worker_connections 1024;
}

#set $my_server_name _ #TODO global variable does not work?

http {
    #DOCKER DNS - using this to resolve docker-compose hosts like 'appsearch', 'kibana' etc
    resolver 127.0.0.11 ipv6=off;

    #include mime.types;
    default_type application/octet-stream;
    #TO read external configuration
    include sites-enabled/*.conf;


    server {  #DEFAULT SERVER
        listen  443 ssl; # Security change
        server_name _;

        include common.conf;
        include /etc/nginx/ssl.conf;

        location / {
            root    html;
            index   index.html  index.htm;
            include common_location.conf;
        }

        error_page  500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

#        location /appsearch { #TODO this /appsearch did not forward. find how to do it.
#              rewrite ^/appsearch(.*) /$1 break;
#              resolver 127.0.0.11 valid=30s ;
#              set $backend http://appsearch:3002;
#              proxy_pass $backend; # Use variable To avoid upstream host not found error.
#        }

    }#server80

    server {
      listen        9200 ssl;
      server_name   _;

      include common.conf;
      include /etc/nginx/ssl.conf;

      location / {
        set $backend http://elasticsearch:9200;
        proxy_pass $backend; # Use variable To avoid upstream host not found error.
        include common_location.conf;
      }
    }#server

    server {
      listen        3002 ssl;
      #server_name   dev.local.org; #TODO yuck, bad to add server name!
      server_name   _;
      include   common.conf;
      include /etc/nginx/ssl.conf;

      location / {
        set $backend http://appsearch:3002;
        proxy_pass $backend; # Use variable To avoid upstream host not found error.
        include common_location.conf;
      }
    }#server

    server {
      listen        5601;
      server_name   _;
      include   common.conf;
      include   /etc/nginx/ssl.conf;

      location / {
        set $backend http://kibana:5601;
        proxy_pass $backend; # Use variable To avoid upstream host not found error.
        include common_location.conf;
      }
    }#server
}

【问题讨论】:

  • 我认为这根本不可能。您可以在一个特定端口上侦听 HTTP 请求或 HTTPS 请求,但不能同时侦听这两种类型。

标签: nginx nginx-reverse-proxy


【解决方案1】:

使用 497 HTTP 错误重定向:(来源:https://meabed.com/http-497-status-code/

在您的 conf 中,您可以添加如下内容:

 
  listen      1234 ssl;

  server_name your.site.tld;

  ssl         on;

  error_page  497 https://$host:1234$request_uri;
  
}```

【讨论】:

  • 哇,真的好用!谢谢,不知道。唯一的,这会产生一个 302 重定向,如果你需要一个 301 重定向,你应该使用 error_page 497 =301 https://$host:1234$request_uri; 代替(已经检查过了)。
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 2012-02-18
  • 2017-07-15
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多