无法使用nginx隐藏位置的端口

时间:2014-03-05 10:01:35

标签: node.js redirect nginx location

我正在尝试使用nginx(v1.5.11)为我的节点项目设置一个域,我已成功将域重定向到Web,但我需要使用3000端口,所以现在,我的Web位置看起来像http://www.myweb.com:3000/当然,我想只保留“www.myweb.com”这样的部分:http://www.myweb.com/

我有搜索并尝试了很多配置,但似乎没有人为我工作,我不知道为什么,这是我的本地nginx.conf文件,我想将http://localhost:8000/文本更改为http://myName/文本,请记住重定向正在工作,我只想“隐藏”该位置的端口。

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


      server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://localhost:8000/;
            proxy_redirect http://localhost:8000/ http://myName/;

        }

        #error_page  404              /404.html;

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

    }

}

概率pd。我正在尝试在我的本地Windows 8机器上修复它,但是如果需要其他操作系统,我的远程服务器可以在Ubuntu 12.04 LTS上运行

谢谢大家。

2 个答案:

答案 0 :(得分:3)

将其添加到您的server区块:

port_in_redirect off;

E.g。

server {
    listen       80;
    server_name  localhost;
    port_in_redirect off;
}

Documentation reference

您还应将server_name更改为myNameserver_name应该是您的域名。

您还应该侦听端口80,然后使用proxy_pass重定向到正在侦听端口8000的任何内容。

完成的结果应如下所示:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include            mime.types;
    default_type       application/octet-stream;
    sendfile           on;
    keepalive_timeout  65;


    server {
      listen       80;
      server_name  www.myweb.com;

      location / {
        proxy_pass http://localhost:8000/;
      }

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

为清楚起见,删除了评论。

答案 1 :(得分:1)

在代理期间隐藏端口需要服务器主体中的这两行:

server_name_in_redirect off;
proxy_set_header Host $host:$server_port;

conf就像:

server
{
listen 80;
server_name example.com;
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
access_log off;
}