Two uwsgi django websites on same domain using nginx - python

I have a django website site1 running on a linux machine.
It has the following config:
site1_nginx.conf
upstream django {
server unix:///var/www/site1/site1.sock;
}
server {
listen 80;
server_name site.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/site1/media;
}
location /static {
alias /var/www/site1/static;
}
location / {
uwsgi_pass django;
include /var/www/site1/uwsgi_params;
}
}
site1_uwsgi.ini
[uwsgi]
chdir = /var/www/site1
module = site1.wsgi
home = /var/www/virtualenv/site1
master = true
processes = 10
socket = /var/www/site1/site1.sock
chmod-socket = 666
vacuum = true
It has a domain site.com.
I have another django website site2 at hand. It's different, has a different virtualenv. I can't add it to the existing website as an app or via django routing. I want the sites to not be related, so that they track sessions separately.
I want the other website site2 available by address www.site.com/site2 or as a subdomain site2.site.com.
Is it possible? What's the best way to do it?
I checked uwsgi emperor mode, but it seems I need different domains for that.
EDIT:
I tried to add the second site as a subdomain.
I created the following configs:
#site2_nginx.conf
upstream django_site2 {
server unix:///var/www/site2/site2.sock;
}
server {
listen 80;
server_name site2.site.com;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /var/www/site2/site2/static;
}
location / {
uwsgi_pass django_site2;
include /var/www/site2/uwsgi_params;
}
}
#site2_uwsgi.ini
[uwsgi]
chdir = /var/www/site2/site2
module = site2.wsgi
home = /var/virtualenv/site2
master = true
processes = 10
socket = /var/www/site2/site2.sock
chmod-socket = 666
vacuum = true
I added a symlink to the nginx config to /etc/nginx/sites-enabled.
site.com works, but site2.site.com says that the page could not be retrieved, so 404.

Related

How to run wordpress on localhost and django on localhost/product?

I have developed a wordpress website for a product.And the product is made using django.So after the user logs in ,i have to redirect him to the django project.I have to achieve this on the same domain.I have nginx installed.
You can try this
www.egwordpress.com (your Wordpress)
egproduct.egwordpress.com (your Django app)
NGinx config
server {
listen 80 default_server;
server_name www.egwordpress.com;
location / {
root /path/to/your/wordpress;
index index.html;
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name egproduct.egwordpress.com;
location /api/ { # your django product app
proxy_pass http://127.0.0.1:5000; # change port following your Django config
}
}

Flask+nginx+uwsgi: only serve url with nginx if flask doesn't have a route for it

nginx config for the server (the main nginx one is the default one on debian 9):
server {
listen 80;
server_name subdomain.domain.com;
include /etc/nginx/mime.types;
location /galleries {
autoindex on;
alias /srv/galleries/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/scraper.sock;
}
}
uwsgi config:
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = /tmp/scraper.sock
chmod-socket = 777
uid = www-data
gid = www-data
vacuum = true
die-on-term = true
plugins = python3
py-autoreload = 1
If I try creating a route for /galleries/whatever, ie like this:
#app.route("/galleries/whatever")
def test():
return "Hello"
I'll just see the indexed files inside /galleries/whatever through nginx instead of going through flask.
Is there a way for me to force nginx to only handle requests if flask returns 404? Alternatively, is there a better way for me to serve files while still having them available under those urls? Keep in mind the /galleries folder is pretty big and generated by another program.
I run the server with "uwsgi --ini server.ini" and nothing else.

NGINX default page is being shown instead of Flask app

I have displayed my NGINX configuration below. One of my Flask applications work (the one above), however the one below does not. The weird thing is, they were both working the other day, now the one below (example2) just returns the NGINX default page.
File structure:
/Signup/
/static/
/assets/
/bootstrap/
/css/
/fonts/
/img/
/js/
/templates/
register.html
main.py
NGINX Config:
server {
listen 80;
listen [::]:80;
server_name example1.domain.net;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
server {
listen 80;
listen [::]:80;
server_name example2.domain.net;
location ^~ /static/ {
proxy_pass http://127.0.0.1:6000;
include /etc/nginx/mime.types;
root /root/Signup/;
}
}
Example 1 works as it should, example2 (second server block does not).
Edit: Thought I should note, I have no prior experience with NGINX, please don't assume.

Nginx Configure Static Site and Django+uWSGI

I am having a bit of trouble getting Nginx to serve a static index.html page, and also to serve a a Django site. Could anyone point me out to what I'm doing wrong?
I have port 80 and 8000 open.
I have spent three days trying to get this working.
Locally I have no problems, then again I'm not using nginx for that.
I placed uwsgi_params within /etc/nginx/uwsgi_params
/etc/nginx/sites-enabled
Here are my symlinks to the actual configuration files
site1.conf -> /home/jesse/projects/site1/conf/site1.conf
site2.conf -> /home/jesse/projects/site2/conf/site2.conf
/home/jesse/projects/site1/conf/site1.conf
This is just a basic static site, but it won't load :(
server {
listen 80;
server_name www.site1.com;
rewrite ^(.*) http://site1.com$1 permanent;
location / {
root /home/jesse/projects/site1/;
}
}
/home/jesse/projects/site2/conf/site2.conf
= The manage.py/wsgy.py is located under /home/jesse/projects/site2/site2/
= This is a Django site using uWSGI, I installed it with $ pip install uwsgi.
server {
listen 80;
server_name www.site2.com;
rewrite ^(.*) http://site2com$1 permanent;
root /home/site2/projects/site2/site2;
location /static/ {
alias /home/jesse/site2/projects/site2/site2/static/;
#expires 30d;
}
location /media/ {
alias /home/jesse/site2/projects/site2/site2/media/;
#expires 30d;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
/home/site2/projects/site2/conf
[uwsgi]
projectname = site2
projectdomain = site2.com
base = /home/jesse/site2/projects/site2/site2
# Config
plugins = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/src/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8000
logto = %(base)/logs/uwsgi.log
# Runs daemon in background
daemonize = /home/jesse/log/$(projectname).log
Nginx Restart
$ sudo service nginx restart
* Restarting nginx nginx [ OK ]
= The site1 produces a Not Found (Not a 404)
= The site2 produces a
I would appreciate any assistance :)
rewrite ^(.*) http://site1.com$1 permanent;
this in site1, is not managed by any server, because no server_name handles site.com (without www).
then again with site2
rewrite ^(.*) http://site2com$1 permanent;
first fix those lines.
the correct way in my opinion is to write a server rule that catches www. names and rewrites them to non www, and place site1.com or site2.com as server_name in the rules you have now, as an example of rewiring
server {
listen 80;
server_name www.site1.com
return 301 http://site1.com$request_uri?;
}

Django keeps changing URL from http://localhost/ to http://127.0.0.1:8080/

As the title described, Django keeps changing my URL from /localhost/ to /127.0.0.1:8080/ which keeps messing up my serving static files by Nginx. Any ideas why its doing this? Thanks!
/**EDIT**/
Here is the Nginx configuration:
server {
listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$
{
root /srv/www/testing;
}
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex on;
}
Here is Apache config file:
<VirtualHost *:8080>
ServerName testing
DocumentRoot /srv/www/testing
<Directory /srv/www/testing>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/testing/apache/django.wsgi
</VirtualHost>
If you're using VirtualHost, you need to set USE_X_FORWARDED_HOST = True in your settings.py
Here's the reference: Django Doc for Settings.py
USE_X_FORWARDED_HOST New in Django 1.3.1: Please, see the release
notes
Default: False
A boolean that specifies whether to use the X-Forwarded-Host header in
preference to the Host header. This should only be enabled if a proxy
which sets this header is in use.
Here's some example code:
import os, socket
PROJECT_DIR = os.path.dirname(__file__)
on_production_server = True if socket.gethostname() == 'your.productionserver.com' else False
DEBUG = True if not on_production_server else False
TEMPLATE_DEBUG = DEBUG
USE_X_FORWARDED_HOST = True if not on_production_server else False
edit2:
http://wiki.nginx.org/HttpProxyModule#proxy_redirect
http://wiki.nginx.org/HttpProxyModule#proxy_pass
What I think is happening is when you use your httpresponseredirect, the HTTP_HOST header is giving it the 127.0.0.1:8080, because of your proxy_pass setting.
Django's HttpResponseRedirect seems to strip off my subdomain?
Django has some methods it always
applies to a response. One of these is
django.utils.http.fix_location_header.
This ensures that a redirection
response always contains an absolute
URI (as required by HTTP spec).
Had the same problem (django redirects got to the browser with the ":8080" appended). After further searching, I found some nginx info. The following fixed it...
In your nginx config, replace...
proxy_redirect off;
with....
proxy_redirect http://127.0.0.1:8080/ http://127.0.0.1/;
Remember to restart your nginx daemon. This causes nginx to strip the 8080 on packets flowing from apache back to the browser. For example a redirect from django via apache, http://127.0.0.1:8080/my/test/file.html will become http://127.0.0.1/my/test/file.html after nginx sends it back to the client.
Now you won't have to modify your django code.

Categories