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.
Related
My server listen 443 port and redirects the requests to another port in the server. Also my server listen 80 port and displays a static content the user when they browse http://www.xxxx.com
But I want also to display static content when user browse https://www.xxxx.com
How can I manage this ? My Nginx config file is ;
server {
listen 443 ssl;
server_name xxxx.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location / {
expires off;
proxy_pass http://backend;
}
}
server {
listen 80;
listen [::]:80;
server_name xxxx.com;
root /var/www/xxxx.com/html;
index index.html;
location / {
try_files $uri $uri/=404;
}
}
I want to display my index.html file when user browse my website with https://www.xxxx.com and my proxy will continue to work at backend
You can invoke a named location as the default action of your try_files statement.
For example:
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
proxy_pass http://backend;
}
See this document for details.
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;
}
}
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.
first time Nginx user. Trying to connect Nginx to Gunicorn running a django app. The app is not actually written yet so once everything is configured, it would just display an under construction page.
I can't seem to get the Nginx configuration correct to pass requests on to Gunicorn then to Django. Instead it always shows the Nginx default page. Greatly appreciate any help!
Here is /etc/nginx/sites-available/mysite:
server {
listen 80;
#server_name <server IP>;
server_name <mysite.com>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/<my user acct>/<my app>/<my site>;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/<my user acct>/<my app>/<my site>/<my site>.sock;
}
}
Here is /etc/nginx/sites-available/default:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 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.html index.htm index.nginx-debian.html;
#server_name <mysite.com> <www.mysite.com>;
#server_name localhost;
server_name _;
#server_name <server IP>;
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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/<mysite.com>/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<mysite.com>/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
i think you should delete the default configuration in sites available and your sites available configuration should be within the sites enabled directory as well.
/sites-available/
|
|-> site.com
|-> site2.com
|-> disabled.com
/sites-enabled/ **this links to the files in /sites-available**
|
|-> site2.com
|-> site2.com
Is it possible to run multiple Django sites on the same server using Nginx and uWSGI?
I suppose it's necessary to run multiple uWSGI instances (one for each site). I copied /etc/init.d/uwsgi to uwsgi2 and changed the port number. But, I got the following error:
# /etc/init.d/uwsgi2 start
Starting uwsgi: /usr/bin/uwsgi already running.
How is it possible to run multiple uWSGI instances?
Thanks
You can create create multiple virtual hosts that allow you to host multiple sites, independent from each other. More info here: http://wiki.nginx.org/VirtualHostExample.
A bit more detailed info here as well on how to setup virtual hosts http://projects.unbit.it/uwsgi/wiki/RunOnNginx#VirtualHosting.
You can run multiple instances of uwsgi using Emperor Mode.
This handles the creation of new worker instances. These instances are brilliantly and hilariously named vassals. Each vassal just needs a config file which is usually placed (or symlinked) in /etc/uwsgi/vassals
For nginx you'll need to create a server block for each host you wish to serve. Just change the server_name directive for each host you want to serve. Here's an example:
#Simple HTTP server
server {
listen 80;
root /usr/share/nginx/www;
server_name host1.example.com;
}
#Django server
server {
listen 80;
server_name host2.example.com;
#...upstream config...
}
Important: Make sure you have specified your host names in /etc/hosts. I found that without doing this my django site was also served on the default server IP despite specifying that it should only be served on a specific host name within my nginx configuration.
I see many suggestions like #donturner's answer. i.e. set two or more different server in nginx configure file. But the problem is each server needs an unique server_name either different domain name or sub-domain name. How about this kind of situation: I want to server two different Django project like this:
www.ourlab.cn/site1/ # first Django project
www.ourlab.cn/site2/ # second Django project
In this way, we can configure all of the settings in one server.
This is my setting in /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
This is my setting in /etc/nginx/conf.d/self_configure.conf
# /etc/nginx/conf.d/self_configure.conf
server {
listen 80;
server_name www.ourlab.cn;
# note that these lines are originally from the "location /" block
root /mnt/data/www/ourlab;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Django media
location /media {
# your Django project's media files - amend as required
alias /mnt/data/www/metCCS/metCCS/static/media;
}
location /static {
# your Django project's static files - amend as required
# first project's static files path
alias /mnt/data/www/metCCS/metCCS/static/static_dirs;
}
location /static_lip {
# second project's static files path
alias /mnt/data/www/lipidCCS/lipidCCS/static/static_dirs;
}
# match www.ourlab.cn/metccs/*
location ~* ^/metccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/metCCS.sock;
}
# match www.ourlab.cn/lipidccs/*
location ~* ^/lipidccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/lipidCCS.sock;
}
}
You also need to change one of the Django project's settings.py file as STATIC_URL = '/static_lip/', so two projects can use their static files separately.
A new finding is nginx can server static files by itself. Even we close uwsgi and Django, we also can use these files through browser.