60

I'm trying to run a minimalist reverse-proxy, and came up with the following :

events {
    worker_connections 4096;
}   

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

`

However, when I access this server, I get the standard "welcome to nginx page", instead of the response from the server running on port 3000.

If I ssh to the machine and run curl http://127.0.0.1:3000/, I get the desired result (and eventually I ran that server on port 80 and it worked fine, so I know it has to do with the reverse proxy config).

agam
  • 5,064
  • 6
  • 31
  • 37

7 Answers7

57

I had exactly the same problem. I just commented out a line in my nginx.conf file:

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

changed to

#include /etc/nginx/sites-enabled/*;
Vijay Boyapati
  • 7,632
  • 7
  • 31
  • 48
  • 9
    What you you say doesn't work. I don't have any `include /etc/nginx/sites-enabled/*;` to comment out anywhere in `/etc/nginx` folder or configs still and experience the issue – Green Feb 24 '17 at 05:25
  • 4
    @Green a bit late, but just came across this issue myself. The include file in my case was `/etc/nginx/conf.d/*.conf` - this includes config for the default site which was conflicting with my site config. In your case it might be an issue with the proxy config itself – codemonkey Mar 17 '17 at 17:53
  • 3
    Had the same issue and commenting out `inculude /etc/nginx/conf.d/*.conf` helped. But the directory `/etc/nginx/conf.d` is empty. Why does this work in such unpredictable way? – Rugnar Oct 11 '18 at 12:43
  • thanks.. i was having issues with setting up ha-bridge on raspberry pi using nginx. this really helped me – j.i.t.h.e.s.h Jan 03 '20 at 16:13
12

Explainng Vijay's post via an answer since exchange will not let me comment yet.

Commenting out the sites-enabled directory is probably required because you are using the standard nginx.conf file. You will notice that line is already within the http directive. If you are using the standard configuration, you are redefining an http directive within another http directive. You could also update your site file to only have the server directive and not the http directive.

Standard-ish nginx.conf file:

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

example compatiable site file within 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;
    }
}
MonomiDev
  • 301
  • 2
  • 8
  • 1
    What you you say doesn't work. I don't have any `include /etc/nginx/sites-enabled/*;` anywhere in nginx folder and experience the issue – Green Feb 24 '17 at 05:24
9

I tried all the above answers, and nothing worked for me, But after pondering for a while, I found out that selinux was set to enforcing and that was causing the issue, as soon as i set selinux to permissive, it worked as expected.

Here is my nginx config:

location / {
    proxy_pass http://127.0.0.1:8080;
    }

Currently selinux is set to enforcing and here is the error i got.

[root@vm1 ~]# getenforce 
 Enforcing
[root@vm1 ~]# 

error image

Once i set selinux to permissive it worked as expected.

[root@vm1 ~]# setenforce 0
[root@vm1 ~]# getenforce 
 Permissive
[root@vm1 ~]# 

successful proxying

Don't forget to make the changes to selinux permanent, otherwise it will not work after the restart.

sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
5

In my experience, adding trailing slash / at the end fixes the problem.

From:

server {
    listen       80;
    server_name  node_servers;
    
    # reverse proxy to nodejs
    location /developV4 {
        proxy_pass http://127.0.0.1:8081;
    }
}

To:

server {
    listen       80;
    server_name  node_servers;
    
    # reverse proxy to nodejs
    location /developV4/ {
        proxy_pass http://127.0.0.1:8081/;
    }
}
Ezekiel Baniaga
  • 853
  • 1
  • 12
  • 26
2

I comment out inculude /etc/nginx/conf.d/*.conf

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

which inside contains the

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;
#}
}
Ben
  • 687
  • 7
  • 15
  • I had to comment out this file but leave `sites-enabled` line intact, since it was pointing to the `sites-available/default.conf` file, in which I was making the changes that did not work at first. – GChuf Jun 21 '20 at 12:51
0

Checking the link from sites-enabled to sites-available might help as well. I had the symlink point to nowhere and therefore nginx did never read the configuration. In my case it was

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

instead of

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

In my case, I always got the default nginx page. As it turned out, the problem was that some tool (I assume Let's Encrypt / Certbot) added some entries to sites-enabled/default that overwrote the ones in my sites-enabled/mysite.com.

Removing the entries in sites-enabled/default resolved the problem.

Daniel Veihelmann
  • 1,275
  • 10
  • 13