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

时间:2017-01-14 11:48:15

标签: nginx docker

我的目标是将http://localhost/web1重定向到http://localhost:5000/。我是nginx的新手,日志给了我。 localhost:5000正在运行。我在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>

日志:

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" "-"

配置:

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/;
  }

}

1 个答案:

答案 0 :(得分:1)

我在{nginx中使用rewrite进行重定向,我认为你应该尝试这样的事情:

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

在我的centos机器上它有效,希望它能帮到你!