HTTPS subdomains redirect - nginx

I have a rule that rewrites all pages and subdomains from port 80 to 443 ssl
server {
listen xx.xx.xx.xx:80 default;
server_name *.xx.net;
return 301 https://xx.net;
}
This is nginx configuration that you see above
how can I add similar one that directs all 443 (https) subdomains (*.xx.net) to just xx.net?
this is for the https (443) listening part
server {
listen xx.xx.xx.xx:443 ssl;
server_name *.xx.net xx.net;
location / {
proxy_pass http://xx.xx.xx.xx:8080;
}
}

Create a separate server block :
server {
listen xx.xx.xx.xx:443 ssl;
server_name *.xx.net;
return 301 https://xx.net;
}
server {
listen xx.xx.xx.xx:443 ssl;
server_name xx.net;
location / {
proxy_pass http://xx.xx.xx.xx:8080;
}
}

Related

Nginx How to prevent processing requests with undefined server names

Nginx is 1.14.1 version
have several virtual hosts and default in the /etc/nging/sites-enabled:
I've tried to configure using this doc: http://nginx.org/en/docs/http/request_processing.html
default
server {
listen 80;
server_name "";
return 444;
}
server {
listen 443 ssl http2 default_server;
server_name _;
ssl_certificate ....
ssl_certificate_key .....
add_header Strict-Transport-Security 'max-age=31536000';
return 444;
}
domain1
server{
listen 80;
server_name domain1;
return 301 https://$server_name;
}
server {
server_name domain1;
listen 443 ssl;
..................
}
but when tried to get access using server IP nginx redirect to domain1. please help what's wrong here. I'd like to deny access by IP to sites and leave only requests with domain name

nginx config catches domains not specified in server_name

I have two configs enabled in my nginx sites-enabled folder.
The first one (my-domain.fr.conf) looks like this:
server {
listen 443 ssl http2;
server_name my-domain.fr;
index index.html;
location / {
root /www/my-domain.fr;
}
include ssl_certif.conf;
}
# HTTP redirect
server {
listen 80 default_server;
server_name my-domain.fr;
location / {
return 301 https://my-domain.fr$request_uri;
}
}
The second one (sub.my-domain.fr.conf) looks like this:
server {
location / {
proxy_pass http://127.0.0.1:8080;
}
include ssl_certif.conf;
server_name sub.my-domain.fr;
listen [::]:443 ssl;
}
server {
if ($host = sub.my-domain.fr) {
return 301 https://$host$request_uri;
}
server_name sub.my-domain.fr;
listen [::]:80;
return 404;
}
I would expect the last one to only catch requests to sub.my-domain.fr subdomains, but instead it catches anything (I have wildcards subdomains set up on my DNS), and even masks my-domain.fr.
How can I make sure it only catches sub.my-domain.fr requests?
I found the reason.
sub.my-domain.fr supports ipv6 (listen [::]:443 ssl;). my-domain.fr doesn't.
I suppose my connection is using ipv6 when it can, and in this case, sub.my-domain.fr is the only match.
Adding ipv6 support (listen 443 ssl => listen [::]:443 ssl;, and listen 80; => listen [::]:80;) in all server entries fix it.

Different prefix path to different hosts with nginx

I would like to achieve the following with nginx. There is one domain with different paths. Each path should proxy the request to a host on different ports:
https://example.com/path1 -> 10.0.0.1:8081
https://example.com/path2 -> 10.0.0.1:8082
https://example.com/path3 -> 10.0.0.1:8083
Something like this did not work:
location /path1 {
proxy_pass http://10.0.0.1:8081;
}
location /path2 {
proxy_pass http://10.0.0.1:8082;
}
location /path3 {
proxy_pass http://10.0.0.1:8083;
}
Is this scenario possible?
EDIT:
This is the config right now on the nginx server
server {
server_name example.com;
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /some/path/fullchain.pem;
ssl_certificate_key /some/path/privkey.pem;
ssl_dhparam /some/path/ssl-dhparams.pem;
location /path1 {
proxy_pass http://10.0.0.1:8081;
}
location /path2 {
proxy_pass http://10.0.0.1:8082;
}
location /path3 {
proxy_pass http://10.0.0.1:8083;
}
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
}
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
return 404;
}
With this config the prefix path gets removed while loading the site.
https://example.com/path1/some/url -> https://example.com/some/url

Nginx will rewirte image domain http to https, But I don't want the static server as https

I have a wordpress website with https protocol by configuring the nginx 301 redirect:
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
And my article has some image links with static server like:
http://yyy.com/1.png
But when i access this article: it will be https://yyy.com/1.png, How do I configure the nginx that can still use http for the image static server?
You would do that using below config
server {
listen 80;
server_name xxx.com;
location ~* \.(png|ico|jpeg)$ {
root <your root folder>;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}

Nginx url rewrite does not work

I want do redirect all requests from my.domain.de to my.domain.com, including rewriting http to https.
The redirection only works with http://my.domain.de which is redirected to https://my.domain.com which is the goal.
When I call https://my.domain.de, it is not redirected.
But when I try to access my.domain.com or http://my.domain.com, the redirect to https scheme fails. Strange, because I used the same rewrite rule for my.domain.de before switching to .com domain and it worked.
Here is my nginx.conf file:
# my.domain.de
server {
listen 80;
server_name my.domain.de;
return 301 https://my.domain.com$request_uri;
}
# my.domain.com
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /path/to/cert;
ssl_certificate_key /path/to/key;
server_name my.domain.com;
# Url rewrite does not seem to work:
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
EDIT:
Formerly I wrote that the redirect from everything at the .de domain works. Unfortunately, it only works, if I enter http://my.domain.de or without http://
When I use https://my.domain.de, it get a warning because of invalid certificate. So there is also something wrong in the rewrite rule for my.domain.de.
EDIT2:
Now I re-installed a cert for my.domain.de, so the only problem I have right know is, that http://my.domain.com is not redirected to https.
Edited nginx.conf:
# my.domain.de
server {
listen 80;
listen 443 ssl;
ssl_certificate /path/to/cert.de;
ssl_certificate_key /path/to/key.de;
server_name my.domain.de;
return 301 https://my.domain.com$request_uri;
}
# my.domain.com
server {
listen 80;
listen 443 ssl;
ssl_certificate /path/to/cert.com;
ssl_certificate_key /path/to/key.com;
server_name my.domain.com;
# Url rewrite does not seem to work:
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
You have SSL enabled for both port 80 and port 443. The use of ssl on; is deprecated, use the ssl option of the listen directive instead.
Use an explicit default server as a "catch-all" to redirect everything that is not my.domain.com and any http address to https://my.domain.com.
server {
listen 80 default_server;
listen 443 default_server ssl;
ssl_certificate /path/to/domain.de/cert;
ssl_certificate_key /path/to/domain.de/key;
return 301 https://my.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
ssl_certificate /path/to/domain.com/cert;
ssl_certificate_key /path/to/domain.com/key;
...
}
Note that one server block uses the old certificate and one server block uses the new certificate.
See this document for more.
As the rewrite does not work for me and I could not set up a default server block because of other servers on the machine, I finally solved the problem by adding two servers, one for port 80 and one for port 443 of my.domain.com. I did not now that this is possible. So this is my new nginx.conf:
# my.domain.de
server {
listen 80;
listen 443 ssl;
ssl_certificate /path/to/cert.de;
ssl_certificate_key /path/to/key.de;
server_name my.domain.de;
return 301 https://my.domain.com$request_uri;
}
# my.domain.com http
server {
listen 80;
server_name my.domain.com;
return 301 https://my.domain.com$request_uri;
}
# my.domain.com https
server {
listen 443 ssl;
ssl_certificate /path/to/cert.com;
ssl_certificate_key /path/to/key.com;
server_name my.domain.com;
}

Resources