nginx simple proxy_pass to localhost not working
ProxyNginxProxy Problem Overview
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).
Proxy Solutions
Solution 1 - Proxy
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/*;
Solution 2 - Proxy
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;
}
}
Solution 3 - Proxy
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;
#}
}
Solution 4 - Proxy
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 ~]#
Once i set selinux to permissive it worked as expected.
[root@vm1 ~]# setenforce 0
[root@vm1 ~]# getenforce
Permissive
[root@vm1 ~]#
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
Solution 5 - Proxy
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
Solution 6 - Proxy
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.