【问题标题】:nginx simple proxy_pass to localhost not workingnginx简单的proxy_pass到localhost不起作用
【发布时间】:2013-02-26 10:01:58
【问题描述】:

我正在尝试运行一个极简的反向代理,并想出了以下内容:

events {
    worker_connections 4096;
}   

http {
    server {
        listen 80;
        location / {
            proxy_pass http://127.0.0.1:3000/;
        }   
    }   
}   

`

但是,当我访问此服务器时,我得到的是标准的“欢迎使用 nginx 页面”,而不是来自运行在端口 3000 上的服务器的响应。

如果我通过 ssh 连接到机器并运行 curl http://127.0.0.1:3000/,我会得到想要的结果(最终我在端口 80 上运行了该服务器,它运行良好,所以我知道它与反向代理配置有关) .

【问题讨论】:

  • 相同的配置在我的 devbox 上运行良好。您的 Web 服务器的网络似乎有问题。您是否尝试过重启网络?
  • 您确定在另一个 conf 文件中没有其他代理指令吗? proxy.conf ?
  • 删除尾部斜线有什么作用吗?
  • 是的,我尝试使用和不使用斜线...将重新启动并重试。
  • 我花了 4 个小时尝试解决这个问题,然后我开始清理 MC 编辑器中的空间选项卡并开始工作。 *******

标签: proxy nginx


【解决方案1】:

就我而言,我总是得到默认的 nginx 页面。事实证明,问题在于某些工具(我假设 Let's Encrypt / Certbot)向 sites-enabled/default 添加了一些条目,覆盖了我的 sites-enabled/mysite.com 中的条目。

删除sites-enabled/default 中的条目解决了问题。

【讨论】:

    【解决方案2】:

    我注释掉包括/etc/nginx/conf.d/*.conf

    # inculude /etc/nginx/conf.d/*.conf
    

    里面包含了

    server {
       listen       80;
       server_name  localhost;
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    #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   /usr/share/nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    }
    

    【讨论】:

    • 我不得不注释掉这个文件,但保持sites-enabled 行不变,因为它指向sites-available/default.conf 文件,我在其中进行了最初不起作用的更改。跨度>
    【解决方案3】:

    检查从启用站点到可用站点的链接也可能会有所帮助。 我的符号链接指向无处,因此 nginx 从未读取配置。 在我的情况下是

    ls -lh /etc/nginx/sites-enabled
    lrwxrwxrwx 1 root root 23 Feb 19 11:11 default -> sites-available/default
    

    而不是

    ls -lh /etc/nginx/sites-enabled
    lrwxrwxrwx 1 root root 23 Feb 19 11:11 default -> ../sites-available/default
    

    【讨论】:

      【解决方案4】:

      通过回答解释 Vijay 的帖子,因为交流不会让我发表评论。

      可能需要注释掉启用站点的目录,因为您使用的是标准 nginx.conf 文件。您会注意到该行已经在 http 指令中。如果您使用的是标准配置,您将在另一个 http 指令中重新定义一个 http 指令。您还可以将站点文件更新为仅包含 server 指令而不包含 http 指令。

      标准的 nginx.conf 文件:

      worker_processes  4;
      
      error_log  /var/log/nginx/error.log;
      pid        /var/run/nginx.pid;
      
      events {
        worker_connections  1024;
      }
      
      http {
      
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
      
        access_log    /var/log/nginx/access.log;
      
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
      
        keepalive_timeout  65;
      
        gzip  on;
        gzip_http_version 1.0;
        gzip_comp_level 5;
        gzip_proxied any;
        gzip_vary off;
        gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
        gzip_min_length  1000;
        gzip_disable     "MSIE [1-6]\.";
      
        server_names_hash_bucket_size 64;
        types_hash_max_size 2048;
        types_hash_bucket_size 64;
        client_max_body_size 1024;
      
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
      }
      

      启用站点中的示例兼容站点文件:

      server {
          server_name {server name};
          listen 80;
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log;
          location / {
              proxy_pass http://example.com:8080;
              proxy_set_header Host $host;
          }
      }
      

      【讨论】:

      • 你说的没用。我在 nginx 文件夹中没有任何 include /etc/nginx/sites-enabled/*; 并遇到问题
      【解决方案5】:

      我遇到了完全相同的问题。我刚刚在我的 nginx.conf 文件中注释掉了一行:

      包括/etc/nginx/sites-enabled/*;

      改为

      #include /etc/nginx/sites-enabled/*;

      【讨论】:

      • 你说的没用。我没有任何include /etc/nginx/sites-enabled/*; 可以在/etc/nginx 文件夹或配置中的任何位置注释掉并遇到问题
      • @Green 有点晚了,但我自己也遇到了这个问题。在我的情况下,包含文件是/etc/nginx/conf.d/*.conf - 这包括与我的站点配置冲突的默认站点的配置。在您的情况下,这可能是代理配置本身的问题
      • 遇到同样的问题并注释掉inculude /etc/nginx/conf.d/*.conf 有帮助。但是目录/etc/nginx/conf.d 是空的。为什么会以这种不可预测的方式工作?
      • 谢谢..我在使用 nginx 在树莓派上设置 ha-bridge 时遇到问题。这真的帮助了我
      猜你喜欢
      • 2020-02-22
      • 1970-01-01
      • 2021-03-03
      • 2018-09-28
      • 2015-07-23
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多