How to use the nginx map directive to redirect multiple domain name with variables - redirect

I have multiple domains as below: www.domain1.com www.domain2.fr www.domain3.com www.domain4.fr www.domain5.biz
I wanted to redirect specific requests coming to the any domain let's say traffic coming to www.domain1.com with specific parameter should be proxy_pass to domain1.domainabc.com
upstream backend {
server <server1 IP>:80;
server <server2 IP>:80;
}
server {
listen 80;
server_name www.domain1.com
server_name www.domain2.fr
server_name www.domain3.com
server_name www.domain4.fr
server_name www.domain5.biz
location / {
common confs
proxy_pass http://backend
}
location ~*/abc-xyz(.*) {
proxy_pass https://$domain.domainabc.com/abc-xyz/$1;
}
location ~/images/(.*) {
proxy_pass https://$domain.domainxyz.com/images/$1;
}
}
where $domain can be domain1, domain2, domain3 etc based on request coming to respective domains...
your earliest help would be apricated.
I tried multiple proxy_pass directtive but could not make it work.

Related

NGINX not respecting server_name regex

I have this nginx config.. i want it to accept all domains that have the word competitions in it and end with .com.au.. I have tested with a domain name that should NOT be accepted but it reaches the application.. is the server_name being ignore because I'm using a proxy?
server {
listen 80 default_server;
server_name ~^(.+)competitions?(.+)\.com\.au;
access_log /var/log/nginx/$host.access.log;
error_log /var/log/nginx/error.log;
if ($host !~* ^www){
rewrite ^/(.*)$ https://www.$host/$1 permanent;
}
location / {
proxy_no_cache 1;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8888;
try_files $uri $uri/ #proxy;
}
location #proxy {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8888;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = #rewrite_proxy;
}
location #rewrite_proxy {
rewrite /(.*) /index.cfm?path=$1;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8888;
}
}
You'd have to remove the default_server from there, because this is a catch-all directive.And you still could setup another one server with the default_server directive, if required.
See How nginx processes a request for a more detailed explanation:
If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.

Nginx Redirect hostname+location

my nginx config uses server_name _; to process all domains in a single server{} block and direct them all to the same codebase sitting in a single folder with a single index.php that sorts out the domain routing. (that's the way the web framework works with 100's of domains within a single cluster - cant change this).
One of the sites has 2 domains. One for each language, let's call them example.com and example.net. This site also has language-specific subfolders, let's call them: /com/ and /net/.
I need to create a redirect that ensures the language subfolder is only loaded on the matching domain.
eg, valid requests:
example.com/com/
example.net/net/
Any other request needs to be redirected based on the subfolder:
example.com/net/ -> example.net/net/
example.net/com/ -> example.com/com/
I cant do it with the framework because the folder contains all static files, so the router is never loaded. Can this be done in the nginx config?
Thank you
update, adding simplified version of my config:
server {
listen 80 default_server;
server_name _;
root /my/app/root;
index index.php index.html;
location / {
try_files $uri /index.php;
}
#PHP
location ~ \.php$ {
try_files $uri = 404;
[fastcgi stuff]
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /apple-touch-icon.png {
access_log off; log_not_found off;
}
location = /apple-touch-icon-precomposed.png {
access_log off;
log_not_found off;
}
}
I think that you will need 1 location block per subfolder (but have not tested it):
location /com {
proxy_pass http://upstream:8888/com;
proxy_set_header Host example.com;
proxy_pass_request_headers on;
}
location /net {
proxy_pass http://upstream:8888/net;
proxy_set_header Host example.net;
proxy_pass_request_headers on;
}
Solved it...
location /com/ {
if ($host = 'www.example.net') {
return 301 https://www.example.com$request_uri;
}
index index.html;
}
And the reverse for the other location/domain

why nginx proxy_pass to sub directory is not working?

I am trying to do a proxy_pass to a subdirectory http://ms.server.com:8085/ms. So whenever anyone hit on http://ms.example.com' it should be redirected tohttp://ms.example.com/ms`. I am trying to do that through below configuration
upstream example {
server ms.server.com:8085;
}
server {
server_name ms.example.com;
location / {
proxy_pass http://example/ms;
}
}
Now I am redirecting to "Nginx Test page".
proxy_pass is used to inform Nginx where to send proxied requests at a host level, and does not account for the URI.
What you want to use instead is a return statement, like so:
upstream example {
server ms.server.com:8085;
}
server {
server_name ms.example.com;
location = / {
return 301 http://ms.example.com/ms;
}
location / {
proxy_pass http://example;
}
}
This will redirect requests to the root URL of the server_name http://ms.example.com using a 301 redirect, and all other traffic will be passed through to the defined upstream.

Nginx redirect for http and https

I am trying to force HTTPS for incoming connections while also redirecting all requests to a certain url.
Desired result:
http://example.com -> https://example.com/dir
https://example.com -> https://example.com/dir
Here is what I believe should work but is saying there are too many redirects.
server {
listen 80;
listen 443 ssl;
server_name example.com;
location / {
return 301 https://$server_name/dir$request_uri;
}
location /dir {
try_files $uri $uri/ /index.php?$args;
}
Any help is much appreciated!
Server that is listening to 443 port shouldn't redirect to itself (return 301 https://...)
Can you try this,
server {
listen 80;
listen 443;
server_name example.com;
location / {
return 301 https://example.com/dir$request_uri;
}
location /dir {
...
...
}
}
Basically the idea is to keep both listen directive in the same server block. The above code is just an excerpt and you need to fill in the missing pieces.You can refer the return directive section of this link for more detail
EDIT: Updated the code with location block.

Nginx: Redirect Conditional on Server Name and Sub Domain for Short URLs

I want to redirect conditionally based on the server name, but where I redirect to also depends on the subdomain. So for example, here is my basic config
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name company.com compa.ny;
ssl on;
ssl_client_certificate /etc/ssl/certs/godaddy_CA.crt;
ssl_certificate /etc/ssl/certs/wildcard.company.com.crt;
ssl_certificate_key /etc/ssl/private/wildcard.company.com.key;
ssl_prefer_server_ciphers on;
root /var/www/company;
access_log /var/log/nginx/nginx.access.log;
error_log /var/log/nginx/nginx.error.log;
client_max_body_size 8M;
location ^~ /application {
proxy_set_header HOST $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8080;
}
}
I want to have something that looks for the short url host "compa.ny" and redirects to "company.com/shortUrldRedirector" and I also want to include the subdomain, so in dev or qa this will work correctly:
https://compa.ny/abc123 -> https://company.com/shortUrldRedirector/abc123
and
https://dev.compa.ny/abc123 -> https://dev.company.com/shortUrldRedirector/abc123
I see there is a $server_name config variable, but how do I accomplish the above redirects respecting the subdomain?
I would use map construction like this:
map $http_host $long_domain {
default company.com;
dev.compa.ny dev.company.com;
compa.ny company.com;
}
server {
...
return 301 https://$long_domain/shortUrldRedirector$request_uri;
}