Nginx/Django SSL Configuration for a specific sub page - redirect

I have an nginx/gunicorn/supervisor/posrgresql server with Django. I server two web pages from this server, with each with its own nginx conf file. I have purchased and downloaded SSL certificates and they are working in some circumstances.
My nginx conf file looks like this:
server {
listen 80;
server_name name.org;
return 301 https://www.name.org;
{
server {
listen 80;
listen 443 ssl;
server_name www.name.org;
ssl_certificate...
}
And so on. What I want to happen is this:
Either
1.) entire site with all pages https
or
2.) one particular sub-page with https, e.g. https://www.name.org/page, no matter how you get there, name.org/page, https://www.name.org/page, www.name.org/page, internal links, etc. The point is I need to serve THAT page over SSL.
Currently, name.org takes you to my home page https. www.name.org does not. I can go to any page on my site, and then enter that page name in the address bar, like https://www.name.org/anypage, and it will reload with the green lock.
I've been looking on stackoverflow, nginx documentation, godaddy (where I purchased my certificates) and everywhere else I can think to search for hours and cannot find anything, so any help will be quite welcome.

Change your first server block to include the www subdomain:
server {
listen 80;
server_name name.org www.name.org;
return 301 https://www.name.org;
}
This will redirect both http://name.org and http://www.name.org to https://www.name.org. After you've done that, you can remove the listen 80; from your second server block, as the first one covers that name / port combination, and you'll only be dealing with the ssl version from then on.

Related

nginx https redirect from IP to IP (not to server name)

I am using a block to redirect all http traffic to https. Simple stuff. However, if I address the server directly by IP, it always redirects to the server_name given. If I do not give a server_name, it does not redirect. This is highly undesirable, as I may be on a LAN where a domain name would go unresolved. Here is my redirect block:
server {
listen 80 default_server; ## listen for ipv4; this line is default and implied
return 301 https://$server_name$request_uri;
}
It redirects to server_name because you set it up like that.
Look at your return 301 https://$server_name$request_uri;. Either you replace $server_name by $host or by the IP you want to redirect to.

AWS : ELB : Route 53 : Nginx : naked domain redirect

Setup
I have two rails app instances running in Opsworks Layer.
I am using Route 53 and an ELB to route traffic to my Layer.
Objective
To redirect naked domain traffic to my www domain.
chicken.com -> www.chicken.com
What I tried
I Alter my nginx conf (on one instance) to solve this problem. I added the following:
server {
listen 80;
server_name chicken.com;
return 301 $scheme://www.chicken.com$request_uri;
}
... rest of config here
Result
Instance is no longer hittable by its IP.
ELB marked the instance I had altered as "Out of Service" since it could no longer be reached by IP (the health check fails).
Question
How can I route naked domains to www domains yet keep my ELB health checks happy?
The ELB health check expects a 200 response. Are you checking against chicken.com instead of www.chicken.com? A redirect page is not sufficient for the health check.
To have both the redirect and provide a 200 response for the ELB check (coming from an internal IP), use the following config:
server {
listen 80;
server_name chicken.com;
return 301 $scheme://www.chicken.com$request_uri;
}
server {
listen 80 default_server;
server_name www.chicken.com;
...
Note the default_server option. If not set, the first server defined is the default one. See the nginx docs for details.

NGINX - redirect all unconfigured subdomains

I'm happy to join community and want to share with you with my little problem.
I've got wildcard entry for example.com in my DNS which points all subdomains to some machine
* IN A 172.172.172.172
While NGINX configuration for this domain contains only actively used subdomain names
server {
listen 10.0.0.1:80;
server_name example.com www.example.com
moskva.example.com www.moskva.example.com
tokyo.example.com www.tokyo.example.com;
...
}
What I want to achieve is directing all unconfigured subdomains like 'mistake.example.com' to specific address.
Is there any elegant way of solving this problem?
Best Regards
Arek
This will instruct the site to redirect any unmatched traffic to example.com
server {
listen 80 default_server;
return 301 http://example.com;
}
You can set a default server section like this:
server {
listen 10.0.0.1:80 default_server;
server_name _;
...
}

NGinx Domain name redirects

Lets say I have a website named xyz.co, I also have other domain names with the same prefix like xyz.com, xyz.it, xyz.co.it
Right now nginx works fine with server_name xyz.co in nginx.conf in port 80 I would want all the other domains to redirect to xyz.co also I would want www.* versions of the above to redirect to xyz.co. How can I get this? Is this nginx webserver level changes? or I need to make this changes in DNS?
UPDATE: I tried this in nginx.conf but no use...
server
{
listen 80;
server_name xyz.co xyz.com, xyz.it, xyz.co.it;
rewrite ^/(.*) http://xyz.co permanent;
}
I first tried posting this question in ServerFault but no response there - https://serverfault.com/questions/453472/nginx-domain-name-redirects
add one server block for all the domain names that need to be redirected. like this:
server {
listen 80;
server_name xyz.com, xyz.it, xyz.co.it;
rewrite ^ http://xyz.co$request_uri permanent;
}
and another server block for the xyz.co domain:
server {
listen 80;
server_name xyz.co;
#other settings
}
this way when you go to one of the domain names that need to be redirected nginx will simply redirect to xyz.co and move into the other server block where you can add all your settings (rootfolder, location blocks, etc)

nginx + varnish redirect server IP to url

i have nginx on port 8080 sitting behind varnish running on port 80. there is only one website on my server. the problem is you can access it by server's IP address too, instead of just url. google indexed this ip and i am afraid of problems with duplicate content.
how do i redirect requests going to IP address to my URL? i tried this code, but it ended up with loop redirects error.
server {
listen 180.10.1.1:80;
server_name 180.10.1.1;
rewrite .* http://www.mysite.com$request_uri permanent;
}
thanks
edit:
rest of vcl
server {
listen 8080;
server_name site.com;
access_log /var/log/nginx/localhost.access.log;
error_page 502 /502.html;
## Default location
location / {
root /home/site.com/public_html;
index index.php;
...
There's a couple of ways to solve this. If Nginx is also serving site.com when you visit the server IP Address then you should adjust the Nginx config so any requests which are directed at the IP address redirect to site.com. Then restart both Nginx and Varnish.
ok the problem was "180.10.1.1:" in the listen directive. i kept there only "listen 80" and now it works fine :)