Nginx URL canonicalization and Subdomains - django

We have nginx sitting in front of apache with django deployed.
nginx has a rule for url canonicalization. All non-www urls are redirected to www with the below rule
server {
listen 80;
server_name xyz.com;
rewrite ^/(.*) http://www.xyz.com/$1 permanent;
}
But now we need to set subdomains. on xyz. But all subdomains are now redirecting to www.xyz.com. What could is the exact rule to be added to redirect only xyz.com to www.xyz.com and not redirect abc.xyc.com.

Type
server_name xyz.com *.xyz.com;

Related

Nginx Proxy_pass to cloudfront URL

We are trying to proxy_pass from nginx to AWS Cloudfront URL.
Something like below.
dare12381.cloudfront.net CNAME to example.com
And, In Nginx proxy config,
location / {
proxy_pass example.com
}
Is it possible to have this setup?
Thanks and Regards!!!

regex infinite subdomains in nginx

I've following nginx config that matches main domain and redirects to new one, also matches 1 level down subdomain and redirects to new domain.
I'm missing case of subdomain of subdomain. e.g. sub.sub.domain.com or sub.sub.sub.sub.domaon.com or infinite levels really.
Any ideas how to safely regex this out?
server {
listen 80;
server_name domain.de.com;
return 301 $scheme://domain.com$request_uri;
}
server {
listen 80;
server_name ~^(?<subdomain>[^.]+)\.domain\.de\.com$;
return 301 $scheme://$subdomain.domain.com$request_uri;
}

Django & Nginx issue with "www" and non-"www" URL

I have a problem understanding how to run my Django app with Nginx (server is Ubuntu).
So, I have a site which allows for user accounts, and it currently has a problem, where both "www" and non-"www" URLs load. This is problematic, because logging-in to one, will not reflect to the other. Besides that, it's also confusing. I want to keep the non-"www" variant.
I followed the instructions here on how to do it using Ngnix:
https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04
And it worked when I tried to load the domain. But when I started the Django app, it stopped working.
I'm really confused on how to get it working, is there some way of running my django app through Nginx?
Thanks!
EDIT: I've included the server config thing (I think)
default
server {
server_name www.nilly.com;
return 301 $scheme://nilly.com$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

How can I use my godaddy domain for my Django App

This situation is some kind of hard to describe. I use nginx and uwsgi to deploy my Django blog on my VPS, it was function well, but I can only use my IP to get access to the app, how can I use my domain without www to setup my app? I set the server_name breakwire.me and set a A record HOST is '#', point to my VPS's IP, but if I visit the breakwire.me, I can only see
"Welcome to nginx"
Here is my nginx conf
# my_blog__nginx.conf
# configuration of the server
server {
the port your site will be served on
listen 106.186.29.228:8000;
# the domain name it will serve for
server_name breakwire.me; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# logs
access_log /home/chen/DjangoProjects/my_blog/logs/access.log;
error_log /home/chen/DjangoProjects/my_blog/logs/error.log;
# Django media
location /media {
alias /home/chen/DjangoProjects/my_blog/media; # your Django project's media files - amend as required
}
location /static {
alias /home/chen/DjangoProjects/my_blog/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 106.186.29.228:8001;
include /home/chen/DjangoProjects/my_blog/uwsgi_params; # the uwsgi_params file you installed
}
}
Change the
listen 106.186.29.228:8000
to
listen 106.186.29.228
or: 106.186.29.228:80
The browsers are always connecting on port 80. Your virtual server in nginx is listening on port 8000, so if you try to open your website with http://breakwire.me:8000/
it's working because you've set the port to 8000 in the listen directive.

Django + uwsgi + nginx + SSL

I am using Django on DotCloud which uses Django on top of uwsgi + nginx. I am trying to redirect all http traffic to https which is leading to a redirect loop. I am using the following http configuration
if ($http_x_forwarded_port != 443) { rewrite ^ https://$http_host/; }
It seems that Django doesn't understand that it is operating on https and the header is not preserved. It redirects https://url.com/ to http://url.com/accounts/login/ which is redirecting again and again leading to a redirect loop. I am not really an expert in nginx and do not understand it well enough. What can I be doing wrong?
In short how do I run redirect http to https in django running on top of uswsgi and nginx.
I needed a bit more to make Django aware that it should be using https.
In settings.py I added
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
And in the nginx configuration
location / {
proxy_set_header X-Forwarded-Proto https;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME https;
uwsgi_pass_header X_FORWARDED_PROTO;
uwsgi_pass unix:///path/to/socket;
}
server {
listen 80;
server_name yourhttphost;
rewrite ^ https://yourhttpshost$request_uri? permanent; #301 redirect
}
server {
listen 443;
server_name yourhttpshost;
........
the rest
........
}
Using "if" in nginx config is a very bad idea!
if ( $scheme = "http" ) {
rewrite ^/(.*)$ https://$host/ permanent;
}