nginx alias : how to alias on one proxy server only? - django

I'm trying to get working a single page application with django/wagtail and react.
But at the moment I'm serving django content via restframework and the request on media/ and djangostatic/ are not working.
I have configured nginx to serve /media and /djangostatic to alias /app/media and /app/static but this is not working properly.
Here is the nginx.conf file:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://172.20.128.3:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20M;
}
location /wagtail {
proxy_pass http://172.20.128.2:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Script-Name /wagtail;
client_max_body_size 20M;
}
location /djangostatic/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
}
When a page in need of /djangostatic is queried, I get the error :
2022/06/12 08:30:10 [error] 22#22: *2 open() "/app/static/wagtailadmin/js/vendor/jquery.datetimepicker.js" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /djangostatic/wagtailadmin/js/vendor/jquery.datetimepicker.js?v=021047c1 HTTP/1.1", host: "localhost", referrer: "http://localhost/wagtail/cms-admin/login/?next=/wagtail/cms-admin/"
So the alias seems to work (it is replaced by /app/media) but it doesn't open the file. I have checked the backend server container and the file exists.
So my question is simple : the /app/static and /media folders are on http://172.20.128.2:8000, which is the reverse proxy of /wagtail location. How to make the alias working on the /wagtail location proxy server but not on others?
I could make two servers with different names but I want this to work on the same server with reverse proxy serving the backend and frontend.

Related

docker + nginx http requests not working in browsers

I have a AWS EC2 instance running Linux with docker containers running gunicorn/django and an nginx reverse proxy.
I don't want it to redirect to https at the moment.
When I try to reach the url by typing out http://url.com in the browser it seems to automatically change to https://url.com and gives me ERR_CONNECTION_REFUSED. The request doesn't show up at all in the nginx access_log.
But when I try to reach it with curl I get a normal response and it does show up in the nginx access_log.
I have ascertained that the django security middleware is not the cause as the HSTS options are disabled.
I've tried clearing the browser cache and deleting the domain from the chrome security policies.
nginx config:
upstream django_server {
server app:8001 fail_timeout=0;
}
server {
listen 80;
server_name url.com www.url.com;
client_max_body_size 4G;
charset utf-8;
keepalive_timeout 5;
location /static/ {
root /usr/share/nginx/sdev/;
expires 30d;
}
location / {
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-Host $server_name;
proxy_pass http://django_server;
}
}
}
What am I overlooking?

Django swagger ui not accessible on nginx

I have setup Django using nginx, gunicorn and postgres as per below url.
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7
Now I am trying to access swagger ui.
Nginx is up and running however showing default page.
When I run the same project using,
python manage.py runserver myip:8000
and then access the same url I can see actual swagger ui with rest end points.
I am not sure what I am doing wrong here.
Here is what I have added to nginx file.
server {
listen 80;
server_name <myipaddress>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/threat-dashboard/backend;
}
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:/tmp/backend.sock;
}
}
There was a mistake in sock file path. I corrected it from /tmp/backend.sock to /tmp/backend/backend.sock and it resolved the issue.

Nginx - How to serve static files with upstream model

I have running my django application in my ubuntu lxc container at 127.0.0.1:8001 with gunicorn and nginx.
This is nginx conf in host:
upstream django_app {
server 10.0.8.100:8001;
}
server {
listen 80;
server_name mysite.com;
location / {
proxy_pass http://django_app;
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;
}
}
And this is nginx conf in container:
server {
listen 80;
location / {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8001;
}
location /static {
alias /opt/static_files/django_app/;
}
}
This configuration is working. I can see my application running from browser, but static files is not loading. I have 404 not found for each static file in browser.
the path /opt/static_files/django_app/ is where all static files collected with collectstatic command.
I could not find a way to serve static files with upstream.
Thank you
/opt/static_files/ should be the same as the STATIC_ROOT setting in your settings.py django file.
Also, change:
location /static {
alias /opt/static_files/django_app/;
}
to:
location /static/ {
alias /opt/static_files/django_app/;
}

Nginx reverse proxy configuration for subdomain with multiple paths

I have a situation here with my Nginx reverse proxy configuration. My distribution is Ubuntu 14.04
I have a domain, let's call it foo.bar.net, and I want the /grafana endpoint to redirect to my grafana server (localhost:3000), the /sentry endpoint to redirect to my sentry server (localhost:9000) and finally, the /private endpoint to redirect to my django server (localhost:8001). I am using gunicorn for the tuneling between django and nginx.
Here is what I tried :
server {
# listen on port 80
listen 80 default_server;
# for requests to these domains
server_name foo.bar.net;
location /sentry {
# keep logs in these files
access_log /var/log/nginx/sentry.access.log;
error_log /var/log/nginx/sentry.error.log;
# You need this to allow users to upload large files
# See http://wiki.nginx.org/HttpCoreModule#client_max_body_size
# I'm not sure where it goes, so I put it in twice. It works.
client_max_body_size 0;
proxy_pass http://localhost:9000;
proxy_redirect off;
proxy_read_timeout 5m;
allow 0.0.0.0;
# make sure these HTTP headers are set properly
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /grafana {
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_read_timeout 5m;
allow 0.0.0.0;
# make sure these HTTP headers are set properly
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /private {
proxy_pass http://127.0.0.1:8001;
}
location /private/static/ {
autoindex on;
alias /home/user/folder/private/static/;
}
}
The server won't even start correctly, the config is not loading.
I would also like the / path to redirect to the private endpoint if possible.
Additionally, I am not even sure where to put this configuration (sites-available/??)
Can anyone help me with that ?
Thanks a lot,
There are some missing semicolons and other syntax errors. Look at main nginx error log for details and fix them one by one.
Where to put that config file depends on your distribution. For some of them it should be sites-available directory and symlink to that file inside sites-enabled directory for quick enabling and disabling sites, if you don't have sites-available and sites enabled directory, you should put it into conf.d dir in your distribution.

How can I use django's built-in server behind nginx?

I'm developing with apache2 ( mpm-worker ) + mod_wsgi behind nginx which is silly since I have to sudo apache2ctl graceful for every update I make in anything but the template files.
My nginx conf is:
server {
listen 80;
server_name site.org;
access_log /www/site.org/log/access.log;
error_log /www/site.org/log/error.log;
location / {
proxy_pass http://127.0.0.1:8080/;
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-Magic-Header "secret";
client_max_body_size 10m;
}
}
Would it be a matter of just binding proxy_pass to 127.0.0.1:3000 if 3000 is the port used by the django server?
Ack, didn't realize it was this easy... I..
copied the server {} settings into another file
changed the port to 3001
changed the server name to dev.site.org
updated my host records in the DNS to point to my server IP
restarted nginx
did manage.py runserver 3001.
All is well :)