Requests with dots at the end gives bad request 400 - django

This is my configuration if I visit
https://www.example.com.
the dot at the end does not work
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/example.sock; # for a file socket
}
# Redirect
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
rewrite ^(.*) $scheme://www.example.com$1 permanent;
}
# SSL Request
server {
listen 443;
server_name www.example.com;
root /var/www/example;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
ssl on;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
ssl_protocols TLSv1.2 TLSv1.1;
# Django media
location /uploads {
alias /var/www/example/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /static {
alias /var/www/example/static; # your Django project's static files - amend as required
expires 7d;
}
location /favicon.ico {
alias /var/www/example/static/img/favicon.ico; # your Django project's static files - amend as required
}
location /robots.txt {
alias /var/www/example/robots.txt; # robots.txt
}
location /ntat {
uwsgi_pass django;
include uwsgi_params;
}
location / {
rewrite ^(.*) http//www.example.com$1;
}
}
# Normal Request
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name www.example.com
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /ntat {
rewrite ^(.*) https://www.example.com$1;
}
# Django media
location /uploads {
alias /var/www/example/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /ntat {
rewrite ^(.*) https://www.example.com$1;
}
# Django media
location /uploads {
alias /var/www/exam/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /static {
alias /var/www/example/static; # your Django project's static files - amend as required
expires 7d;
}
location /favicon.ico {
alias /var/www/example/static/img/favicon.ico; # your Django project's static files - amend as required
}
location /robots.txt {
alias /var/www/example/robots.txt; # robots.txt
}
location /timepass {
alias /var/www/timepass; # Snake Game
}
# Finally, send all non-media requests to the Django server.
location / {
rewrite ^(.*) https://www.example.com$1;
uwsgi_pass django;
include uwsgi_params;
uwsgi_read_timeout 180;
}
}

Ensure default_server as needed
Don't use rewrite where you can simply return.
Always specify server_name to ensure there is no different priority between servers
Note http//www.example.com$1; (missing :)
The www.example.com. is different server name for NGINX than www.example.com, but you don't have a server that listens specifically for the dotted domain, thus it's best to use default server to mark the server block which will ensure matching properly.
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/example.sock; # for a file socket
}
# Redirect for example.com -> www.example.com (both ports)
server {
listen 80;
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
return 301 $scheme://www.example.com$request_uri permanent;
}
# SSL Request
server {
listen 443 default_server;
server_name www.example.com;
root /var/www/example;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
ssl on;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
ssl_protocols TLSv1.2 TLSv1.1;
# Django media
location /uploads {
alias /var/www/example/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /static {
alias /var/www/example/static; # your Django project's static files - amend as required
expires 7d;
}
location /favicon.ico {
alias /var/www/example/static/img/favicon.ico; # your Django project's static files - amend as required
}
location /robots.txt {
alias /var/www/example/robots.txt; # robots.txt
}
location /ntat {
uwsgi_pass django;
include uwsgi_params;
}
location / {
rewrite ^(.*) http//www.example.com$1;
}
}
# Normal Request
server {
# the port your site will be served on
listen 80 default_server;
# the domain name it will serve for
server_name www.example.com
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /ntat {
rewrite ^(.*) https://www.example.com$1;
}
# Django media
location /uploads {
alias /var/www/example/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /ntat {
rewrite ^(.*) https://www.example.com$1;
}
# Django media
location /uploads {
alias /var/www/exam/uploads; # your Django project's media files - amend as required
expires 7d;
}
location /static {
alias /var/www/example/static; # your Django project's static files - amend as required
expires 7d;
}
location /favicon.ico {
alias /var/www/example/static/img/favicon.ico; # your Django project's static files - amend as required
}
location /robots.txt {
alias /var/www/example/robots.txt; # robots.txt
}
location /timepass {
alias /var/www/timepass; # Snake Game
}
# Finally, send all non-media requests to the Django server.
location / {
rewrite ^(.*) https://www.example.com$1;
uwsgi_pass django;
include uwsgi_params;
uwsgi_read_timeout 180;
}
}

Related

Nginx not serving files from /var/www/html after deploying Django app

I have a Django application stored at /var/www/w_gm .
Now I have used Nginx + Gunicorn to deploy it.
My default conf file at
root#dev:/etc/nginx/sites-enabled# ls
default w_gm
Default conf file :
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
w_gm conf file :
server {
listen 80;
server_name 119.00.00.100;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
}
Now the issue is that, when am tying in my IP address i.e, it's redirecting me to the Django app and it's working perfectly fine. But my other files which are in var/www/html doesn't get served i.e if I have <ip.addr>/work1 , it gives me error.
Now if I edit the w_gm conf file and add a suffix, let's say server_name 119.00.00.100/abc; , which obviously is wrong, my /var/www/html files start working.
I need a solution where if I type in <ip.addr>/something, then it should redirect to Django app, else serve the files which are in var/www/html.
You currently have two servers, but it sounds like you only want one server.
<ip.addr>/something is ambiguous, so you need Nginx to look for files in one root and send requests to the proxy only if they are not found.
You would need to combine your two server blocks into something like this:
root /var/www/html;
location / {
try_files $uri $uri/ #proxy;
}
location ~ \.php$ {
...
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location #proxy {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
This assumes that the /static/ URI is unambiguous, and that no URIs ending with .php are sent to the proxy. See this docuemnt for more.

How to configure nginx for websocket. I have django for REST in backend. The standard configurations i found over net wont work for nginx websocket

I have nginx to server to browser. I want nginx to serve for websocket requests from browser. Nginx has internally proxy to django (gunicorn uwsgi) using proxy configs. I am not able to set up the config in nginx for websocket. Tried different configs from internet but no success. My default file in nginx config file :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/gmc/dist;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404 /index.html index.js;
}
location ~ /redfish.* {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
}
These are the basic settings you need to apply for your django-project:
server {
listen 80;
server_name **Server Name or IP**;
#Website LOGO:
location = /favicon.ico { access_log off; log_not_found off;}
#Static Files like CSS, JS
location = /static/ {
root /home/hitshell/website/project/
}
#Media Files like Images, Videos etc.
location = /media/ {
root /home/hitshell/website/project/
}
#proxy
location = / {
include proxy_params;
proxy_pass http://unix:/home/hitshell/website/project/project.sock;
}
}
REFERENCE:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn
https://www.shellvoide.com/hacks/installing-django-application-with-nginx-mysql-and-gunicorn-on-ubuntu-vps/
The First one has some missing configurations in the middle of the tutorial.

Django server - 502 error bad gateway

I have a centOS with a virtualenv, i can use the project in my localhost , but when the project is upload to a server give an error :
502 - bad gateway
I think the problem probably is in my nginx file.
server {
listen 80;
server_name www.site.com.br site.com.br;
root /var/www/html/agrodez/src/;
if ($http_host != "www.site.com.br") {
rewrite ^ http://site.com.br$request_uri permanent;
}
location /static/ {
alias /var/www/html/site/src/sistema/static/;
}
location /{
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
}
I don't know much about django, but recently I had to help a team with server configuration, and I've used the following nginx virtualhost configuration:
upstream django.local {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name site.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://django.local/;
}
location /static {
autoindex on;
# this line would be STATIC_ROOT dir
alias /var/www/html/agrodez/src/staticfiles;
}
location /media {
autoindex on;
# this line would be MEDIA_ROOT dir;
alias /var/www/html/agrodez/src/media;
}
# If you want nginx logs, then can add these
error_log /var/log/nginx/site.com_error.log;
access_log /var/log/nginx/site.com_access.log;
}
You can give it a try.

django: nginx static content doesn't work

I'm using django with nginx and gunicorn. nginx is supposed to serve the static content, but css, images and js files are not loaded in the browser. Why is that?
I've substituted my Django project's name with domain.
/etc/nginx/sites-enabled/domain.tld
server {
listen 80;
server_name 127.0.0.1;
access_log /srv/domain/access.log;
error_log /srv/domain/error.log;
location /static {
alias /srv/domain/collected_static;
}
location / {
proxy_pass http://127.0.0.1:8888;
}
}
/etc/nginx/conf/nginx.conf
user http;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include /etc/nginx/sites-enabled/*;
}
gunicorn.conf.py
bind = "127.0.0.1:8888"
logfile = "/srv/domain/gunicorn.log"
loglevel = "info"
workers = 3
Excerpt from Django settings
DEPLOY_PATH = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(DEPLOY_PATH, 'collected_static')
STATIC_URL = '/static/'
EDIT:
Output from the machine (links to pastebin):
ps aux | grep nginx
ls -l *.log
Your configuration looks correct. As long as the files are really collected, the Django and Gunicorn configurations have nothing to do with the static files serving. The following possibilities come to my mind:
The files are not collected into your collected_static directory yet (./manage.py collectstatic)
Nginx has no read access to the files
You use an old nginx version that has problems with your current configuration. You should use a current 1.x version, if you're on Debian, use the Deb repository from nginx.org.
If permissions aren't the problem, check the nginx access file to see if the requests are really reaching Nginx. Then check the nginx error log to see if any errors were logged.
As a sidenote (but unrelated), I recommend putting some proxy headers in your / location configuration and moving the app server configuration into a separate section, e.g.:
upstream app_server {
server localhost:8888 fail_timeout=0;
}
server {
...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
break;
}
}

How to serve django media files via nginx ?

I'm new at Nginx, I've successfully bound my django project to Nginx. However I can't serve my static files and I guess I set my media folder's location wrongly. Here is my file tree:
root_directory
my_django_project
...
manage.py
app1
app2
media
admin
css
js
...
And my nginx.conf goes like :
server {
listen 192.168.1.9:80;
server_name localhost;
# site_media - folder in uri for static files
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off; # po co mi logi obrazków :)
expires 30d;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log;
}
}
When I open my admin page, all css pages give 404 error. Can you tell me that how can I set my media path correctly ?
Here's a example of how I have my nginx servers setup
server {
server_name example.com www.example.com;
location /static {
autoindex on;
alias /home/myusername/myproject/static/;
}
location /media {
autoindex on;
alias /home/myusername/myproject/media/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)
The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.
This ServerFault question may help.
You have set this:
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
This means that the location will be xxx/media/media. Nginx will go to [...]/portal/media and look for a folder called media.
It should rather be like this:
location /media/ {
root /home/nazmi/workspace/portal;
}
Also you should remove the trailing slash.
If the media file isn't serving, try to set media location as below in the conf file inside the sites-enabled directory. This works for me.
location /media {
root /home/username/projectname/;
The following code works for me:
server {
server_name example.com www.example.com;
location /static {
autoindex on;
**alias /home/myusername/myproject/;**
}
location /media {
autoindex on;
**alias /home/myusername/myproject/;**
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I bold the different parts according to the previous answer.
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
Try this code in nginx config to serve static and media files for a django applications (without autoindex on settings)