My goal is to serve the staticfiles to nginx on ubuntu from a docker django app container without using docker nginx
I setup the reverse proxy to django all works fine in dev mode but when i turn Debug to False nginx doesn't recognize the staticfiles path
here's a screenshot
Here's my dockerfile for django app
FROM python:3.9
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN pip install pipenv
RUN pipenv install --system --deploy --ignore-pipfile
EXPOSE 8000
ENTRYPOINT ["python", "manage.py"]
CMD ["runserver", "0.0.0.0:8000"]
And here's my nginx config
upstream django {
server 127.0.0.1:8001;
}
server {
server_name django.com;
listen 80;
listen 8000;
client_max_body_size 100M;
location / {
proxy_pass http://django;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
proxy_read_timeout 1000;
proxy_connect_timeout 1000;
proxy_send_timeout 1000;
}
}
To have NGINX serve your staticfiles, your config needs to be edited. How do you expect nginx to find your files otherwise?
Luckily, it's a simple edit. :)
upstream django {
server 127.0.0.1:8001;
}
server {
server_name django.com;
listen 80;
listen 8000;
client_max_body_size 100M;
location /static {
alias /PATH/TO/STATIC_FILES;
}
location / {
proxy_pass http://django;
...
}
}
Don't forget to configure your static root, and run:
py manage.py collectstatic
This will make sure Django's default CSS/JS is included in your staticfiles.
When DEBUG=True, your staticfiles will be served by Django. This is not good, and can lead to some serious perfomance and security problems.
Edit:
Alternatively, have a look at Django-Whitenoise. It's great for serving staticfiles.
http://whitenoise.evans.io/en/stable/
Related
I have a server code that uses flask socketio. The server is started by the command: 1gunicorn --worker-class eventlet -w 1 module:app`
I have the following nginx configuration:
server {
listen 80;
server_name A.B.C.D;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:5000;
}
location /socket.io {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
}
This is from the official documentation of Flask Socket io, here
My two endpoints are working just fine. However, the socket endpoint runs fine when run manually(i.e. me running the command: python index.py), the problem comes when I want to run it via nginx and gunicorn.
With nginx:
Failed to load resource: net::ERR_CONNECTION_REFUSED
I have put code which is from the documentation, confs from the documentation and nothing worked.
I'm beginner in Django and I've finished my first project.
I have an Ubuntu server in Digital Ocean and this is what I've done:
My project nginx configuration file:
server {
server_name domain.com;
access_log off;
location /static/ {
alias /opt/myenv/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
My project is located in /opt/myenv/myenv/
When I execute gunicorn myproject.wsgi it looks like it's running
Listening at: http://127.0.0.1:8000 (1481)
But when I access into my IP, I just see an Welcome message from Nginx.
What is happening?
(Sorry my bad english)
I can't comment (rep points) so will post here.
You have Gunicorn bound to :8000 but have Nginx looking at :8001
Did you put this config in sites-available and symlink to sites-enabled, then remove "default" from sites-enabled? (If it's showing welcome I assume not)
I don't see where you are directing Nginx to listen:
server {
listen 80 default_server;
listen [::]:80;
...
Any reason you are not setting up Gunicorn using a unix sock instead and configuring Nginx to that? Here's a typical config for this:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
I'm trying to serve both a Django and a Flask site using gunicorn and nginx. The Django site is currently available at example.com, and I'd like for the Flask site to be availabe at myapp.example.com .
My nginx configuration file is as follows for the Django site:
cat /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com;
location /static {
alias /home/me/sites/example.com/static;
}
location / {
proxy_set_header Host $host;
proxy_pass http://unix:/tmp/example.com.socket;
}
}
I also have a second nginx config file for the Flask site:
cat /etc/nginx/sites-available/myapp.example.com
server {
listen 80;
server_name myapp.example.com;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm pretty sure that I want to change my proxy_pass variable, but am not sure what I should change it to.
My gunicorn file currently is as follows:
description "Gunicorn server for example.com"
start on net-device-up
stop on shutdown
respawn
setuid me
chdir /home/rowan/sites/example.com/source
exec ../virtualenv/bin/gunicorn \
--bind unix:/tmp/example.com.socket \
example.com.wsgi:application
Do I need a second gunicorn config file for the Flask site? Which I would then put in /etc/init/ and start through the sudo start gunicorn-myapp.example.com command?
I am running my flask project in nginx. This is the conf file
server {
listen 80;
server_name site.in;
root /root/site-demo/;
access_log /var/log/site/access_log;
error_log /var/log/site/error_log;
location / {
proxy_pass http://127.0.0.1:4000/;
proxy_redirect http://127.0.0.1:4000 http://site.in;
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;
}
}
When i tried to put the expires part for static files into the conf it failed. I read that this may be due to the fact that the static files are served by flask rather than nginx. If so what changes should i bring to the above conf file so that the static file serving can be done by nginx for my project.
As per the answer i changed the conf as below. Now all static file shows 403 error.
server {
listen 80;
server_name site.in;
root /root/site-demo/;
access_log /var/log/site/access_log;
error_log /var/log/site/error_log;
location / {
proxy_pass http://127.0.0.1:4000/;
proxy_redirect http://127.0.0.1:4000 http://site.in;
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;
}
location /static {
alias /root/site-demo/static;
autoindex on;
expires max;
}
}
Add this to your nginx configuration:
location /static {
alias /path/to/your/static/folder;
autoindex on;
expires max;
}
EDIT
nginx requires the whole tree to be readable and not just where your root starts in nginx.conf. So the command
sudo chmod -R 777 /root/site-demo/static
should solve the permissions problem. But, I think, is not a good thing - for security reasons - to put your site in the /root directory of your web server. Usually a site is put under the /var/www folder.
P.S.
The chmod -R 777 command gives owner, group and others permission to read, write and execute files in a folder and in all its subfolders.
check your nginx configuration here:
/etc/nginx/sites-enabled/
/etc/nginx/sites-available/
I was experiencing the same issue you describe
Noticed that I had several configuration files
Leaving only one config file fixed
This site is also helpful:
https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/
if you run on server or docker ,you should do like that:
server {
listen 443;
server_name sample.xx.code;
location /{
proxy_pass http://127.0.0.1:5000;
}
location /static {
proxy_pass http://video/static;
expires max;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
}
I'm new at this and have only been using nginx to serve static files. I have now installed flask and gunicorn. If I run gunicorn -b 127.0.0.2:8000 hello:app and then wget it from the server it works well. If I try to access it from a browser, however, it returns a 404 error (I am running this on a server that hosts a wordpress site which is locatet at root).
The flask app:
from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
#app.route('/')
def hello():
return "Hello world!"
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
app.run()
And the relevant part of my nginx configuration:
location /flask {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_\
for;
proxy_pass http://127.0.0.2:8000;
proxy_redirect off;
}
I hope this is all the relevant info. If not, do tell. Thanks!
This is how I serve my flask apps in Nginx:
Run gunicorn daemonized using a socket:
sudo gunicorn app:app --bind unix:/tmp/gunicorn_flask.sock -w 4 -D
Related nginx config:
upstream flask_server {
# swap the commented lines below to switch between socket and port
server unix:/tmp/gunicorn_flask.sock fail_timeout=0;
#server 127.0.0.1:5000 fail_timeout=0;
}
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
client_max_body_size 4G;
server_name example.com;
keepalive_timeout 5;
# path for static files
location /static {
alias /path/to/static;
autoindex on;
expires max;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://flask_server;
break;
}
}
}
}