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

5
  • 404 for what? You are only passing /static to your app. Commented Dec 6, 2016 at 5:26
  • Im trying to pass a html file and a python .py file Commented Dec 6, 2016 at 5:30
  • that's as clear as mud Commented Dec 6, 2016 at 5: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 Commented Dec 6, 2016 at 5:37
  • my Nginx config: 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. Commented Dec 6, 2016 at 8:43

2 Answers 2

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/
}
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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...
The python code is just a .py script to run the Python Webserver on port 80 to say "Hello World".
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: "127.0.0.1:8080", host: "IP:80"
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: stackoverflow.com/questions/25235453/…
|
0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.