nginx redirect www and non www - django

Hi im having trouble with my first Django site and not sure what im doing wrong.
I basically want to redirect http:// www. example.com and http:// example.com and https:// www. example.com all to > https://example.com
The redirect from http:// example.com seems to work.
This is what my conf file looks like.
server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
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;
}
}
server {
listen 80;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 $scheme://$host$request_uri;
}
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}
What am i doing wrong?
The answer from Richard has solved my problem.

Related

Django+Wordpress+Nginx reverse proxy apache

I have 2 machines one is django (https://orgofoods.com) and another one is wordpress (https://blog.orgofoods.com). Django is running with nginx, gunicorn and the configuration goes like this
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name orgofoods.com;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location /blog {
proxy_pass https://blog.orgofoods.com;
}
location /blog/wp-content {
proxy_pass https://blog.orgofoods.com/wp-content;
}
location /blog/wp-includes {
proxy_pass https://blog.orgofoods.com/wp-includes;
}
location /blog/wp-login.php {
proxy_pass https://blog.orgofoods.com/wp-login.php;
}
location /blog/wp-admin {
proxy_pass https://blog.orgofoods.com/wp-admin;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/orgofoods.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/orgofoods.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
}
server {
if ($host = orgofoods.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name orgofoods.com;
return 404; # managed by Certbot
}
but when i try to access orgofoods.com/blog it is returning 404 error, and the request is handled by nginx where as it needs to be handled by apache, please any one can shed some light on it.
dev tools network screenshot
Thanks in advance
p.s.: i followed this tutorial (https://jeffreyeverhart.com/2016/12/11/wordpress-nginx-proxy-server-subdomain-subdirectory)

502 bad gateway after redirecting to https django

I've been trying to encrypt my website with https, but when redirecting I get 502 bad gateaway, I use Digital Ocean with nginx on Ubuntu 14 and django:
Here is my server config:
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
server_name = programmationetia.com;
ssl_certificate /etc/letsencrypt/live/programmationetia.com/fullchain.pem ;
ssl_certificate_key /etc/letsencrypt/live/programmationetia.com/privkey.pem;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass https://app_server;
}
}
server {
listen 80;
server_name programmationetia.com;
return 301 https://$server_name$request_uri;
}
I followed Sentex tutorial on encrypting with ssl.
Thank you for helping

Why does my facebook login redirect url still using my docker container name?

I am developing django web application, using django-allauth in a docker environment. In my facebook login redirect URI, I already set the value to be my website URL (https://whizkids.id). But somehow in the redirect URL return by facebook, it change to my docker container name: web.
https://web.facebook.com/v2.12/dialog/oauth?redirect_uri=https%3A%2F%2Fweb%2Faccounts%2Ffacebook%2Flogin%2Fcallback%2F&client_id=2180006765347725&scope&state=xp0mwKV8NW5w&response_type=code&_rdc=1&_rdr
The part redirect_uri=https%3A%2F%2Fweb%2Faccounts%2Ffacebook%2Flogin%2Fcallback%2F should be redirect_uri=https%3A%2F%2Fwhizkids.id%2Faccounts%2Ffacebook%2Flogin%2Fcallback%2F
I am using nginx + letsencrypt configuration below:
upstream web {
ip_hash;
server web:8000;
}
server {
listen 8000;
server_name whizkids.id www.whizkids.id;
return 301 https://$host$request_uri;
location ~ /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name whizkids.id www.whizkids.id;
ssl_certificate /etc/letsencrypt/live/whizkids.id/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/whizkids.id/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers '...';
location /static/ {
autoindex on;
alias /src/static/;
}
location / {
proxy_pass http://web/;
}
}
Is there any configuration that I missed?
Referring this answer you need to set the Host header, either static or dynamic, such as
upstream web {
ip_hash;
server web:8000;
}
server {
listen 8000;
server_name whizkids.id www.whizkids.id;
return 301 https://$host$request_uri;
location ~ /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name whizkids.id www.whizkids.id;
ssl_certificate /etc/letsencrypt/live/whizkids.id/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/whizkids.id/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers '...';
location /static/ {
autoindex on;
alias /src/static/;
}
location / {
proxy_pass http://web/;
proxy_set_header Host $host; // or whizkids.id
}
}

Django How to redirect only example.com to https and *.example.com to http?

I need http://example.com to redirect to https://example.com. Whereas http://www.example.com, http://api.example.com must not redirect i.e, subdomains need not redirect to https.
I can understand the configuration here by looking at it. But don't know to move further. Please help me.
My configuration so far is:
sites-available/default.py
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
return 301 https://example.com$request_uri;
}
server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 443; # <-
ssl on; # <-
ssl_certificate /etc/ssl/example_cert_chain.crt; # <-
ssl_certificate_key /etc/ssl/example.key; # <-
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/static/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; # <-
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
Edit this:
server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
return 301 https://example.com$request_uri;
}
Into:
server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
if ($http_host = "example.com") {
rewrite ^ https://example.com$request_uri permanent;
}
}

Redirect from https to https doesn't work. Can't find server, ngnix

I tried to run site on https instead of http and therefore to create a redirect. At the moment site works at https adress. If I enter http adress, redirect to https takes place but it says it can't find server. What am I doing wrong? Commented is what I have also tried. I don't really understand why server_name is _ in second configuration(which works), because it's a pre-installed image configuration on VPS
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
#listen 80;
#server_name solomon;
#return 301 https://closer.com$request_uri;
#server_name solomon;
#listen 80;
#return 301 https://closer.com;
}
server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443;
ssl on;
ssl_certificate /etc/ssl/simple_academy_cert_chain.crt;
ssl_certificate_key /etc/ssl/server.key;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
Here's how I handle it on my server:
server {
listen 80;
server_name mysite.com www.mysite.com;
return 301 https://$server_name$request_uri;
}
All HTTP traffic gets redirected to HTTPS.