簡體   English   中英

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

[英]nginx proxy from port 80 to other ports

我的目標是將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/;
  }

}

我在 nginx 中用於重定向目的的rewrite ,我認為你應該嘗試這樣的事情:

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

在我的 centos 機器上它可以工作,希望它會幫助你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM