nginx简单的proxy_pass到localhost无法正常工作

时间:2013-03-11 09:27:02

标签: proxy nginx

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

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上运行了该服务器并且它工作正常,所以我知道它与反向代理配置)。

5 个答案:

答案 0 :(得分:32)

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

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

更改为

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

答案 1 :(得分:9)

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

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

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

答案 2 :(得分:0)

我注释掉了/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;
#}
}

答案 3 :(得分:0)

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

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

答案 4 :(得分:-1)

检查从已启用站点到可用站点的链接也可能有所帮助。 我的符号链接指向无处,因此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