1

I'm trying to setup a python server using port 8080 and having nginx to proxy from port 80 to 8080.

Right now I have

python -m SimpleHTTPServer 8080 

running, but for some reason I can not get Nginx to proxy it. I keep getting a "404 Not Found" error. (nginx/1.10.2) Here is the config I have on Nginx.

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location /static/ {
#    root   /usr/share/nginx/html;
    root   /home/ec2-user;
    index  index.html index.htm;
    proxy_pass   http://localhost:8080;
}

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

Thank you

Devon
  • 307
  • 3
  • 13
  • 404 for what? You are only passing `/static` to your app. – jordanm Dec 06 '16 at 05:26
  • Im trying to pass a html file and a python .py file – Devon Dec 06 '16 at 05:30
  • that's as clear as mud – jordanm Dec 06 '16 at 05:31
  • lol.. sorry Im new to Nginx.. I have a html file, python server is displaying on port 8080. I want the outside world to use port 80 and Nginx to proxy it to port 8080.. hope that helps – Devon Dec 06 '16 at 05:37
  • my Nginx config: http://pastebin.com/4cc2vEKj . It works with Gunicorn but it doesn't matter - it works the same way with any server because it uses socket to connect. – furas Dec 06 '16 at 08:43

2 Answers2

2

You need to remove the index directive inside your location block:

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location /static/ {
#    root   /usr/share/nginx/html;
    root   /home/ec2-user;
    # index  index.html index.htm; # It is looking for an index
    proxy_pass   http://localhost:8080;
}

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

The index makes nginx look for an index before the proxy_pass happens. Commenting it or removing it will resolve the issue.

Also, the root is not needed either. Just this actually:

locatioin /static/ {
    proxy_pass   http://localhost:8080/
}
Tim
  • 2,139
  • 13
  • 18
  • Could you update your question with the Python code? Remember that `proxy_pass` is going to send `/static` to your application, so remember that you need to respond to that URI. – Tim Dec 06 '16 at 17:25
  • I made the adjustments you mentioned. The python web server conf is correct, but when I try to proxy it, I still getting the 404 Not Found error... – Devon Dec 06 '16 at 17:36
  • The python code is just a .py script to run the Python Webserver on port 80 to say "Hello World". – Devon Dec 06 '16 at 17:38
  • I getting this errror in the log, [crit] 2562#2562: *1 connect() to 127.0.0.1:12891 failed (13: Permission denied) while connecting to upstream, client: IP, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "IP:80" – Devon Dec 06 '16 at 18:00
  • 1
    Oh, it is `selinux` - I just disabled selinux out of the box because it is annoying (bad practice) but you could also run this: `/usr/sbin/setsebool httpd_can_network_connect true` Here: http://stackoverflow.com/questions/25235453/nginx-proxy-server-localhost-permission-denied#25277699 – Tim Dec 06 '16 at 18:08
  • Thank you Everyone.. it was SELinux – Devon Jan 10 '17 at 00:08
0

You can actually embed Python code in nginx with the Python module https://github.com/arut/nginx-python-module