skip to Main Content

I have developed a python flask application and I’m trying to publish it on Linux Server as WebService. It doesn’t have any interface it just return string or something like this. I have developed it on Windows and my code was like in the below:

if __name__ == '__main__':
    app.run(port=80, debug=True)

Then I have connected to linux server via SSH.

I installed my codes to -> /var/www/myapp/code
Then I created a myapp.conf config file to -> /etc/nginx/sites-enabled:

server {
        listen 127.0.0.1:80;
        server_name x.x.x.x;

        root /var/www/myapp/code/main.py;

        passenger_enabled on;
}

Then I run "python main.py" in /var/www/myapp/code/ Then I get:

return getattr(self._sock,name)(args)
socket.error: [Errno 98] Address already in use

When I change listen 127.0.0.1:80; to 80 it works but i can’t send any request. I’m trying to send via postman as http ://x.x.x.x:80/myurl

How can I publish it correctly? I want to serve it on Linux Server and send request from my windows post man application.

Thanks.

3

Answers


  1. use :

    server {
            listen 0.0.0.0:80;
            server_name x.x.x.x;
    
            root /var/www/myapp/code/main.py;
    
            passenger_enabled on;
    }
    

    for change your ip

    Login or Signup to reply.
  2. You are trying to use same port for your flask app and nginx

    Use a different port for flask app

     if __name__ == '__main__':
            app.run(port=8080, debug=True)
    

    Also, nginx can’t talk to python apps directly, instead you have to proxy the requests to flask server

    server {
            listen 80 default_server;
            server_name _;
            passenger_enabled on;
            location / {
               proxy_pass  http://127.0.0.1:8080;
            }
    }
    
    Login or Signup to reply.
  3. Adding on @Conans-Answer

    Request you to refer the Documentation:- https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/

    server {
    listen 80;
    
    server_name _;
    
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
    
    location / {
        proxy_pass         http://127.0.0.1:8000/;
        proxy_redirect     off;
    
        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;
    }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search