Nginx redirect http traffic to https - http

I have a domain which work only in our VPN network.
Recently we enabled the SSL on that and its working fine with https, but now i want to redirect all my http traffic also to https.
For this i added below block along with ssl block but still http request is not re-directing to https.
server {
listen 80;
server_name domain_name.com www.domain_name.com;
return 301 https://$domain_name$request_uri;
}
server {
server_name example.com;
access_log /var/log/nginx/example-access.log;
error_log /var/log/nginx/example-error.log;
root /var/www/html/web;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$uri?$args;
}
}
I tried with my domain_name,localhost with port number also, nothing works for me.

Related

Nginx redirect HTTP to HTTPS: "Connection is not secure"

What I'm trying to do, is have incoming HTTP requests be redirected to HTTPS with SSL. I followed the video by NGINX (https://www.youtube.com/watch?v=X3Pr5VATOyA). My config looks like this:
server {
listen 80;
server_name xzntrc.xyz 58.111.95.242;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xzntrc.xyz 58.111.95.242;
# SSL Certificates
ssl_certificate /etc/nginx/ssl/example.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
# Removing File Extensions
location / {
if ($request_uri ~ ^/(.*)\.html) {
return 302 /$1;
}
try_files $uri $uri.html $uri/ =404;
root /home/xzntrc/web;
index index.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
As far as I'm aware, everything is fine. I used OpenSSL as the video used, it worked, I was able to get HTTPs working but no matter what browser, from any location, just tells me that the connection is not secure. However I can still press proceed anyways, I'd rather not have to do that like most sites. I tried changing the SSL certificates to the Cloudflare Origin Server certificates, yet the issue persists.
If it's needed, my records look like this:

Change port for http to https -- Nginx

Sorry for limited understanding on Nginx, Iam new to Nginx.
I have a webapp running on React and Nginx. Recently I received the SSL certificates for my website. I tried to configure the website and it worked partially. The problem is when I tried to open "https://example.com", the SSL certificates are visible here but its showing nginx default home page. While when I open "http://example.com" it shows all the webcontent.
I attempted to:
change the port from 80 to 443
Reinstall nginx.
But nothing seems to work. Here is my nginx confs at the moment:
/etc/nginx/sites-available/example.org
server {
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.cer;
ssl_certificate_key /etc/nginx/ssl/example.key
root /var/www/html;
server_name example.org;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://example.org;
}
/etc/nginx/conf.d/www/example.org.conf
server {
listen 80 default_server;
server www.example.org;
location / {
root /usr/share/nginx/html;
index index.htm; index.html;
}
}
Note: I reload nginx at every new attempt.
Please help where am I going wrong.
Keeping just 1 file for config works for the above problem. I kept the "default" conf at "/etc/nginx/sites-available"

How to forcefully redirect from http to https on AWS ec2 with Nginx

I am using AWS ec2 for Hosting my Web Application which is running on PHP.I have SSL for it but due to some of my old backlinks which are having http request. When this request gets called my server blocks http request with response of Error 400, So i want to forecefully redirect that http request to https.
I have tried adding return 301 https://$host$request_uri; to my Nginx Config File but it is not working
My Nginx Config File
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;
}
return 301 https://$host$request_uri;
}
What can i do so that the request from http get redirected to https....?

NGINX Forward HTTPS from any domain to specific URL

I am implementing an internal DNS server for block specific DNS requests to malicious websites, using a DNSRBL list against bind9. Whenever there's a match, the DNS server responds with the IP of an internal NGINX server that serves a block page.
Example, when the internal client requests http://www.badsite.com/ the DNS server responds with 192.168.0.100 as an example, which is the IP of the NGINX server. Then NGINX uses a 301 to forward the request to an HTTPS site which serves the block page message to the end user.
That works well using a simple NGINX config:
server {
listen 80 default_server;
server_name _;
return 301 https://block.xyz.com;
}
server {
listen 443 ssl;
server_name block.xyz.com;
ssl_certificate /etc/letsencrypt/live/block.xyz.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/block.xyz.com/privkey.pem;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
The issue I'm having is when the client requests an HTTPS site, i.e.:https://www.badsite.com/ . I would like to forward any incoming SSL/443 requests to https://block.xyz.com. I've tried adding the following directive:
server {
listen 443 ssl default_server;
server_name _;
ssl_certificate /etc/letsencrypt/live/block.xyz.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/block.xyz.com/privkey.pem;
return 301 https://block.xyz.com;
}
And I get the typical SSL error saying the certificate doesn't match the domain: NET::ERR_CERT_COMMON_NAME_INVALID, which is understandable. The same thing happens when I change the directive from return to rewrite:
...
rewrite ^ https://block.xyz.com;
....
How would I go about adding a directive in NGINX to accomplish this? This guide (https://sweetcode.io/ad-blocking-with-local-dns-servers-and-nginx/) provided me a way to do the http side for implementing something similar for Ad Blocking, but doesn't speak to https requests.
Any clues?
In your server block try adding:
if ($host != "block.xyz.com") {
rewrite ^/(.*) https://block.xyz.com/$1 permanent;
}

Nginx subdomains not working

I don't know what's wrong. I don't get any warnings in logs. I've similar config to this
How to exclude specific subdomains server_name in nginx configuration
I want to create subdomain us.example.io I'm using ping to check it
ping us.example.io
ping: cannot resolve us.example.io: Unknown host
nginx.config
server {
server_name *. us.example.io us.example.io;
listen 80;
root /usr/share/nginx/html/bint;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
server_name us.example.io;
listen 80;
return http://www.google.com;
}
The problem has nothing to do with nginx. The error suggests that you haven't configured a DNS record for the domain.

Resources