67

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).

7
  • The same config works fine on my devbox. Seems something wrong with your web server's network. Have you tried to restart your network?
    – Chuan Ma
    Mar 11, 2013 at 13:27
  • are you sure you don't have other proxy directive in another conf file ? proxy.conf ?
    – Pixou
    Mar 11, 2013 at 16:57
  • Does removing the trailing slash do anything?
    – Bart
    Mar 14, 2013 at 22:54
  • Yes, I tried with and without the slash ... will restart and try again.
    – agam
    Mar 19, 2013 at 8:23
  • I spend 4 hours try to fix this issue, then i just start cleaning spaces tabs in MC editor and its start working. *******
    – user956584
    Aug 3, 2016 at 10:33

7 Answers 7

59

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/*;
6
  • 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, 2017 at 5: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, 2017 at 17:53
  • 4
    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, 2018 at 12:43
  • thanks.. i was having issues with setting up ha-bridge on raspberry pi using nginx. this really helped me Jan 3, 2020 at 16:13
  • I had the same issue with showing the default page, but I had defined my proxy_pass in sites-available/ and linked in sites-enabled/, so I used the include /etc/nginx/sites-enabled. Thanks to @Rugnar I commented out the include /etc/nginx/conf.d/*.conf just for testing and it worked!! And I do not understand it either because my conf.d/ directory is empty. Dec 6, 2023 at 16:50
13

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;
    }
}
1
  • 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, 2017 at 5:24
10

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/;
    }
}
3
  • 2
    thanks bro you saved literally my hours Jan 23, 2023 at 22:41
  • 1
    I cannot believe this worked, it is very strange how the local environment and cloud environment handles this differently.
    – Kai Durai
    Sep 19, 2023 at 21:51
  • This fixed it for me! Thank you. I was going around in circles before I tried this! Mar 1 at 23:10
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
3

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;
#}
}
1
  • 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, 2020 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
-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.

1
  • Please specify which entries should be removed.
    – Elijah
    Feb 21, 2022 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.