nginx simple proxy_pass to localhost not working

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

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

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;
#}
}

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