简体   繁体   English

nginx 代理从 80 端口到其他端口

[英]nginx proxy from port 80 to other ports

My goal is to redirect http://localhost/web1 to http://localhost:5000/ .我的目标是将http://localhost/web1重定向到http://localhost:5000/ I am new to nginx and the logs gives me.我是 nginx 的新手,日志给了我。 localhost:5000 is up running. localhost:5000 正在运行。 I am running this on docker if it could be part of the problem (nginx:alpine)?如果它可能是问题的一部分,我正在 docker 上运行它(nginx:alpine)?

$curl localhost:5000

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

log:日志:

2017/01/14 11:32:24 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /web1 HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "localhost"
172.18.0.1 - - [14/Jan/2017:11:32:24 +0000] "GET /web1 HTTP/1.1" 502 537 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" "-"

config:配置:

server {
  listen       80;
  server_name  localhost;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
  }

  # redirect server error pages to the static page /50x.html
  #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }

  location /web1 {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:5000/;
  }

  location /web2 {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:5001/;
  }

}

I use for rewrite for redirect purposes in nginx, and i think you should try something like this:我在 nginx 中用于重定向目的的rewrite ,我认为你应该尝试这样的事情:

location /web1 {
    rewrite  ^/(.*)$  $scheme://127.0.0.1:5000/$1  permanent;
}

On my centos machine it works, hope it will help you!在我的 centos 机器上它可以工作,希望它会帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM