49

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

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并且它工作正常,所以我知道它与反向代理配置有关)。

4

5 回答 5

48

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

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

变成

#include /etc/nginx/sites-enabled/*;
于 2013-04-26T22:25:48.130 回答
11

通过回答来解释 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;
    }
}
于 2014-12-31T04:01:26.233 回答
2

我注释掉 inculude /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;
#}
}
于 2019-11-24T05:42:14.490 回答
0

检查从启用站点到可用站点的链接也可能会有所帮助。我的符号链接指向无处,因此 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
于 2016-02-19T10:13:40.530 回答
-1

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

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

于 2021-04-06T05:49:32.643 回答