【问题标题】:Can't hide location's port with nginx无法使用 nginx 隐藏位置的端口
【发布时间】:2014-04-07 07:31:18
【问题描述】:

我正在尝试使用 nginx (v1.5.11) 为我的节点项目设置域,我已成功将域重定向到网络,但我需要使用 3000 端口,所以现在,我的网络位置看起来像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 上运行

谢谢大家。

【问题讨论】:

  • 似乎您的 proxy_pass 指向您的服务器侦听的同一个 localhost:8000。将监听端口更改为 80。

标签: node.js redirect nginx location


【解决方案1】:

将此添加到您的 server 块中:

port_in_redirect off;

例如

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2018-06-30
      • 2020-11-28
      相关资源
      最近更新 更多