Waitress Nginx Unable to serve static files with relative path - python

I am using waitress as a WSGI to serve my Django application and I am using nginx as a reverse proxy. When I am giving the absolute path to the static files it works fine but if I try to give relative path it doesn't work.
server {
listen 8000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
access_log "logs/nginx-access.log";
error_log "logs/nginx-error.log";
location /static/ {
alias "C:/Users/mateen/Documents/MDA-Configuration-Server/static/";
#alias "static/";
}
location / {
proxy_pass http://127.0.0.1:8008;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
The commented part in location /static/ is what I want to make it work. I have also made sure that the file location is correct. I gave the relative paths in access log and error log and they work fine. Is it something I am missing here?

Related

'Welcome' message from Nginx shown when I run my wsgi Django project + Nginx + Gunicorn

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

Serving multiple sites with gunicorn and nginx?

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?

Change static file serving to nginx from flask?

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

Unable to load static files on django with gunicorn ,nginx on Google compute engine

I am trying to deploy a sample app on google compute engine and have a centos7 instance setup. I am following this tutorial.
Everything is working fine but the static files are not loading. I have used the setting :
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
and ran collectstatic command. The static file directory is created in the project and have all the static files.
Here is my nginx.conf:
server {
listen 80;
server_name My_External_IP ;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
alias /home/user/myproject/static/;
}
location / {
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_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
Here is a sample error from nginx error.log:
2015/06/29 06:49:49 [error] 3566#0: *3 open() "/home/user/myproject/static/admin/css/login.css" failed (13: Permission denied), client: <<MY_IP>>
, server: <<SERVER_IP>>, request: "GET /static/admin/css/login.css HTTP/1.1", host: "<<SERVER_IP>>", referrer: "http://<<SERVER_IP>>/admin/login/?next=/admin/"
Please suggest what to do.
Edit:
I am also getting this message when I run systemctl status nginx:
Failed to read PID from file /run/nginx.pid: Invalid argument

Django using Nginx to serve static content

I am trying to configure nginx to serve the static content for my django project on a remote VPS. I'm using the following configuration for my nginx instance:
server {
server_name myVPSip;
access_log off;
location /static/ {
alias /usr/local/pcat/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"';
}
}
I created the config file in ../nginx/sites-available/, linked it to /sites-enabled/, and restarted nginx however when I hit myip:8001/static I get a django 404.
You shouldn't access your website by 123.123.123.123:8001 because that's the port django is using. You are supposed to access 123.123.123.123 (that is port 80) where nginx is running.

Categories

Resources