nginx sub-domain mapping - django

I want to open xyz.abc.com in the browser but internally(using python-django), I want to map this to abc.com/xyz
The following nginx conf code works, but I don't want to redirect the user to this new url (abc.com/xyz)
server {
listen 80;
server_name xyz.abc.com;
location / {
rewrite ^ http://abc.com/xyz;
break;
}
I have tried a lot of things including using proxy_pass but it's not working.
How can I solve this?
Thanks.

You can convert any 3rd level domain to 2nd level:
server {
listen 80;
server_name ~^(?<domain>.*)\.abc\.com;
location / {
proxy_pass http://abc.com/$domain$request_uri;
break;
}
In your case try the next:
server {
listen 80;
server_name xyz.abc.com;
location / {
proxy_pass http://abc.com/xyz$request_uri;
break;
}
About the request_uri: http://wiki.nginx.org/HttpCoreModule

Related

Nginx subdomain running the wrong web application

so I have a ubuntu server that run two different website with two different domain:
www.firstwebsite.com
www.secondwebsite.com
But when I create an AAA record to create a subdomain with the first domain (like this)
demo.firstwebsite.com
If I go to this subdomain it automatically run the application website of my other domain (www.secondwebsite.com)
I tried creating a specific socket&service file for the subdomain for it but it still run the web application of the second domain.
Im not sure what is causing that and how to fix that? thank you
nginx config file
server {
listen 80;
server_name firstwebsite.com;
return 301 $scheme://www.firstwebsite.com$request_uri;
}
server {
listen 80;
server_name www.firstwebsite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/firstwebsite;
}
location /media/ {
root /var/www/firstwebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/firstwebsite.sock;
}
}

How to set up Django with nginx reverse proxy

I have a Django project that I have up and running with the development server at 127.0.0.1:8888. I'm trying to get it to run on my vps with nginx, so I can see it at example.com/djangoApp.
Here's my nginx.conf:
server {
server_name example.com;
location /otherLocation/ {
proxy_pass http://127.0.0.1:10000;
}
location /djangoApp/ {
proxy_pass http://127.0.0.1:8888;
}
When I navigate to example.com/djangoApp, it throws an error: "Using the URLconf defined in djangoApp.urls, Django tried these URL patterns, in this order:
/admin
The current path, djangoApp/, didn't match any of these."
Can I modify the root url in settings.py to mitigate this?
I fixed this by adding to nginx.conf:
location /djangoApp {
rewrite ^/djangoApp/(.*) /$1 break;
proxy_pass http://127.0.0.1:8888;
}
Thanks to this SO exchange.
server {
server_name example.com;
location /otherLocation/ {
proxy_pass http://127.0.0.1:10000/;
}
location /djangoApp/ {
proxy_pass http://127.0.0.1:8888/;
}
}
The above should work. You are missing the '/' at the end of the proxy_pass url
Alternatively, you can do
server {
server_name example.com;
location /otherLocation {
proxy_pass http://127.0.0.1:10000;
}
location /djangoApp {
proxy_pass http://127.0.0.1:8888;
}
}

Website goes unresponsive after directing to https in nginx server

I have a web application in django framework and I have setup an nginx server to serve the site. I have also setup SSL into the site. The site works fine with both http and https.
Now I want to direct all http requests to https so my users always use the secure version.
Here is my nginx config:
server {
listen 80;
listen 443 ssl;
server_name site.com www.site.com;
ssl_certificate /path/to/SSL;
ssl_certificate_key /path/to/SSL/key;
location = /favicon.ico { access_log off; log_not_found off; }
location /site_media/static/ {
alias /home/user/folder/static/dist/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/site.sock;
}
}
Now when I insert a 301 redirect to https and restart the server, the site goes unresponsive.
return 301 https://$server_name$request_uri;
into my
server { ... }
Any idea how to fix this issue, any suggestions would be highly appreciated.
Placing an unprotected return statement into the server block will attempt to redirect both the http and https sites, resulting in a loop. You could place the return statement inside an if block and detect when the protocol is not https, or the more common solution is split the configuration across two server blocks, for example:
server {
listen 80;
server_name site.com www.site.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name site.com www.site.com;
ssl_certificate /path/to/SSL;
ssl_certificate_key /path/to/SSL/key;
location = /favicon.ico { access_log off; log_not_found off; }
location /site_media/static/ {
alias /home/user/folder/static/dist/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/site.sock;
}
}

www prefix not showing site

I'm trying to make my site work with the www prefix. I can only reach the site without the prefix. I am using Nginx+Django in digitalocean. Here is my site config file:
/etc/nginx/sites-enabled/mysite
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name .example.com;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/proyect/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/proyect/static;
}
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;
}
}
I have tried without success these:
# rewrite ^ http://example.com$uri permanent;
# rewrite ^/(.*) http://example.com/$1 permanent;
# server_name example.com www.example.com;
What am I doing wrong?
For my site I set it up so all www traffic was sent to the non-www address of my site by having two server blocks, one to redirect www traffic to the non-www address and one the handle the non-www traffic. I think the opposite should work for your situation.
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
...
}
I'm not sure if this is the best way to go about it as I'm still pretty new to Nginx.
The $host variable can be used to check for the occurrence of a "www" prefix. You could add this to the server section of the config to remove it:
server {
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ https://$host_without_www$1 permanent;
}
....
goto : DNS -> SELECT DOMAIN -> ADD RECORD -> Select record type "A"
hostname : www
IP Adress : your droplets IP

django setting up ngnix with static and media files served in a sub domain

I managed to get ngnix configured and its running when i try doing things like mocorner.com/static but when I try static.mocorner.com its taking me to landing page.
Below is my ngnix config file, can anyone advise about how I can resolve this issue? plus how can I separate the logs for each static and media?
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 80;
access_log /home/mocorner/moapps/mocorner/logs/static-nginx-access.log;
error_log /home/mocorner/moapps/mocorner/logs/static-nginx-error.log;
location / {
proxy_pass http://backend;
include /etc/nginx/proxy.conf;
}
location /static {
root /home/mocorner/moapps/mocorner/app/static;
}
location /media {
root /home/mocorner/moapps/mocorner/media;
}
}
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.mydomain.com
access_log /home/mocorner/moapps/mocorner/logs/static-nginx-access.log;
error_log /home/mocorner/moapps/mocorner/logs/static-nginx-error.log;
location / {
proxy_pass http://backend;
include /etc/nginx/proxy.conf;
}
}
server {
listen 80;
server_name media.mydomain.com
access_log /home/mocorner/moapps/mocorner/logs/media-access.log;
error_log /home/mocorner/moapps/mocorner/logs/media-error.log;
root /home/mocorner/moapps/mocorner/media;
}
server {
listen 80;
server_name static.mydomain.com
access_log /home/mocorner/moapps/mocorner/logs/static-access.log;
error_log /home/mocorner/moapps/mocorner/logs/static-error.log;
root /home/mocorner/moapps/mocorner/app/static;
}