I am deploying Flask application with Nginx and uWSGI for first time. Nginx will listen to port 8000 and WSGI will listen to 8081. I followed the instructions given in problem statement, but the NGINX fails to start.
Problem statement: Deploy a simple flask application with nginx and uwsgi.
Commands for configuring nginx server as per Problem statement:
- sudo vi /etc/nginx/nginx.conf
- The I was asked to include /projects/challenge/deploy.config in Virtual Host configs.
- sudo service nginx restart
For the code, this is what I did: wsgi.py
# Put your code here
from projects.challenge import app
if __name__ == "__main__":
app.run()
deploy.conf
server {
listen 8000;
server_name localhost:8081;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080;
}
location /Hello {
alias /project/challenges/api.py
}
}
The test code given in the question for testing deploy.conf is:
def test_conf_file_contents(self):
with open('deploy.conf', 'r') as f:
content = f.read()
assert "location /Hello" in content
assert "server localhost:8081" in content
assert "listen 8000" in content
api.py
from flask import Flask, request, make_response
app = Flask(__name__)
app.secret_key = "Thisisyoursecret"
# Create a simple endpoint /Hello with return message "Welcome to your flask application"
@app.route('/Hello')
def hello():
res=make_response("Welcome to your flask application")
return res
As per instruction in the question, I included deploy.conf in Virtual Host Configs of nginx.conf file as follows:
http{
...
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /projects/challenge/deploy.conf
}
uwsgi.ini
(uwsgi)
socket=127.0.0.1:8081
wsgi-file=wsgi.py
The error is:
user@workspacede6jnv452qg2cr45:/projects/challenge$ sudo service nginx restart
* Restarting nginx nginx (fail)
user@workspacede6jnv452qg2cr45:/projects/challenge$
On going to path etc/nginx and starting:
user@workspacede6jnv452qg2cr45:/etc/nginx$ sudo service nginx restart
* Restarting nginx nginx
nginx is failing to restart when I include deploy.conf in Virtual configuration of nginx.conf. But it starts when I do not include deploy.conf.
I might be making some obvious mistake, I went through Google and Youtube but could not figure out what to fix. No solution worked. I suspect the deploy.conf file but I have no clue how to fix the issue.
Kindly suggest me what to do.