Nginx forward from domain A port 8080 to domain B port 80 - redirect

Currently I'm able to do required redirrects from server A to server B, both listening port 80 default / location.
What I need is additional forward of all requests sent to server A port 8080 non-default location /loc1 to Server B port 80 (the same) location /loc1
server {
listen 80;
server_name 1.1.1.1;
return 301 $scheme://1.1.1.2$request_uri;
}
Is this only doable by adding additional server section like below?
Edit:
server {
listen 8080;
server_name 1.1.1.1;
location /loc1 {
return 301 $scheme://1.1.1.2:80$request_uri;
}
}
Any help is appreciated.
Edit: Having two above "server" sections in config worked flawlessly.

Yes, you'll need an additional server block to listen on 8080. In the second config you've provided, I think there's a typo. 8080 shouldn't be in server_name. Also, having the return statement there means that all requests to 1.1.1.1:8080 will get redirected to 1.1.1.2:80. If you only want to redirect if /loc1 is in the URI, then add a location block as seen below:
server{
listen 8080;
server_name 1.1.1.1;
#rest of your config
location /loc1 {
return 301 $scheme://1.1.1.2:80$request_uri;
}
}

Related

NGINX Redirection domain does not work

I'd like to redirect one of my domain to another one. Both domains are configured on Digital Ocean where I set up the DNS to point to the IP adresse of the server
nginx
server {
server_name domain1.com;
rewrite ^/(.*)$ http://domain2.com/$1 permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name domain2.com www.domain2.com;
......
}
nginx-t runs fine.
Problems
The address www.domain2.com is working but www.domain1.com is not redirected. What am I missing ?
I cleared the cache of Chrome and Safari and it is fine.

Nginx: Only use server block if location matches

I have a catch all server block like this:
server {
listen 80 default_server;
server_name _;
location /blog{
# pass request to ghost
}
location /{
# pass request to custom node app
}
}
It passes to the custom node app, which verifies the requested domain, protocol, and path, and issues a single 301 redirect if necessary; we do this to minimize 301 redirects, for SEO purposes.
I also need my ghost blog to only be served at https://www.exmaple.com/blog. I added the following block:
server {
listen 80;
server_name example.com;
location /blog {
return 301 https://www.example.com$request_uri;
}
}
so that requests to the naked domain would get redirected. But now requests to example.com return the default Nginx index.html page. How do I prevent that? I'd like to avoid using if.
You need to have a catch-all in the naked domain server block that routes to the node application
server {
listen 80;
server_name example.com;
location /blog {
return 301 https://www.example.com$request_uri;
}
location /{
# pass request to custom node app
}
}

NGINX Redirect all paths to a specific path

I have a main domain (example.com) and multiple domains (example.net, example1.info, example2.info) pointing to one location in NGINX configuration like so:
server {
server_name example.com *.com *.net *.info;
root /home/example.com/public;
}
And I want to redirect a specific path /login to example.com/login, how do I accomplish it? If I do
server {
server_name example.com *.com *.net *.info;
root /home/example.com/public;
location = /login {
return 301 https://example.com/login;
}
this work for all domains, except example.com/login because it keeps redirecting to itself.
What is the correct way of creating a redirections from a specific path on all sites to my chosen path?
The simplest logic to redirect from one site to another is to isolate the target site with its own server block. Common configuration can be imported into both server blocks by using an include statement.
I would use a default server block rather than a long list of wild cards.
Something like this:
server {
listen 80 default_server;
listen 443 ssl default_server;
include /path/to/common/config;
location = /login {
return 301 https://example.com/login;
}
}
server {
listen 443 ssl;
server_name example.com;
include /path/to/common/config;
}
See this document for details.

nginx redirect rule is redirecting everything to https even for other ports

Hello I have this config
server {
listen 82;
server_name myapp.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name myapp.mydomain.com
# ... remain of the stuff here
}
Before that I had other websites running in ports: 80, 3000 etc... now when I access http://myapp.mydomain.com automatically redirects me to my app (as is I were invoking port 82) and if I try to access another app running on 3000 port it tries to rewrite the https://myapp.mydomain.com:3000 as well... if I use the ip it works as expected (not the ssl part).
Full config can be found at:
https://gist.github.com/angvp/363f50ff8b8d345126adaf1595cd2523
Any ideas?
Ok after I start digging I had this on my nginx conf:
add_header Strict-Transport-Security max-age=15768000;
This is a security measure but that was causing all the subdomains even on different ports will try always https .. the correct way should be to have different subdomains per vhost per port..

Is it feasible to redirect all possible similar domain to one domain in Nginx

I have below configuration but it only redirects my specified urls to target.fi,I want to make it more general and for any possible domain that contain "target" it will be redirected to my domain.
server {
listen 80;
server_name abctarget.fi bctarget.fi targetbcd.fi bc.target.fi;
return 301 $scheme://target.fi;
}
How to make it like server_name *target*.fi;
server {
listen 80;
server_name ?;
return 301 $scheme://target.fi;
}
You could use a wildcard name. See this documentation.