nginx Redirect subfolder to root domain - nginx

I want to redirect the subfolder and all contents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
My server configuration is like below:
server {
listen 80 default_server;
root /home/vishant/devcenter/wava-v1.1/HTML;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name baetter.l;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
i have found similar problem solved using htaccess here
But how can i achieve in nginx??

One of a number of solutions is:
location ^~ /ubb/ {
return 302 /;
}
The ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future. See this document for details.
The return directive is documented here.

Related

Site is not redirecting to backend node NGINX config

I have a react frontend with a node backend but my nginx setup is currently not working. What I want to do is redirect https://sales.example.com/api/stats/get_customer_count/2323232 to http://127.0.0.1:3000/stats/get_customer_count/2323232(Node backend server runs on http://127.0.0.1:3000) but I keep getting 404 errors stating that the api path is not found. Not sure how to go about fixing this. Appreciate it. Thanks
server {
root "/home/sales/frontend/dist";
index index.html index.htm;
server_name sales.example.com;
error_log /var/log/nginx/error.log;
listen 80;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location ~* ^/api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
This seems to be answered here already, and the explanation written by #Dayo is good.
Translated it to your example, it looks like this: (notice, the main difference is the tailing slash in the proxy pass)
location ~* ^/api/ {
proxy_pass http://127.0.0.1:3000/;
}
But please, read through the linked answer, before copying this over.

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Nginx configuration for displaying contents in home directory

The requirement is to display contents in local user's home directory when you enter /~user.
www.blahblahblah.com/~user
I've created a local user with home directory and configured Nginx like that:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm test.html;
# Make site accessible from http://localhost/
server_name localhost;
location ~* /\~(.*)/$ {
root /home/$1/;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
But I still got 404 not found error.
The problem is that the root and the URI are concatenated to obtain the request filename, so you are asking nginx to deliver root /home/user/~user/.
The solution is to use a rewrite ... break to sanitise the URI. You can extend the regular expression to capture both elements and then use the named captures in a root and a rewrite ... break statement:
location ~* /\~(?<username>\w+)(?<userpath>/.*)$ {
root /home/$username;
rewrite ^ $userpath break;
}

Nginx config file

How does nginx figure I have a conflicting server name?
[warn] conflicting server name "" on 0.0.0.0:80, ignored nginx.
I wouldn't care, except it only seems to be serving files from project.ca and completely ignores projectfinancial.com...
server {
root /var/www/project/infrastructure/project/static-sites/projectlabswebsite;
index index.html
server_name projectfinancial.com *.projectfinancial.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
server {
root /var/www/project/infrastructure/project/static-sites/project-website;
index index.html
server_name project.ca *.project.ca;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
The answer is:
There is a missing semi colon after index index.html
And for those interested, you can set server_name to .project.ca instead of project.ca *.project.ca as they are both the same.

How can I mask a Nginx virtual host redirect?

I was asked to make a rewrite rule to redirect a virtual host subdomain to another virtual host managed by the same Nginx server. That works fine but I'd like to mask the URL redirect in the browser.
There is a configuration file for each virtual host: subdomain.mydomain.com.conf configuration file was made from original www.mydomain.com.conf with required substitutions.
This is www.mydomain.com.conf virtual host configuration file:
server {
listen 127.0.0.1:80;
listen 443 ssl;
server_name www.mydomain.com;
access_log /var/log/nginx/www.mydomain.com-access_log main;
error_log /var/log/nginx/www.mydomain.com-error_log;
root /var/www/html/www.mydomain.com;
index index.php index.html index.htm;
...
location / {
try_files $uri $uri/ /index.php?$args;
}
...
I made a Nginx rewrite rule to redirect URLs beginning with www.mydomain.com/subdomain to subdomain.mydomain.com:
location /subdomain {
rewrite ^/$ subdomain.mydomain.com$request_uri last;
}
I found a link that explains how to make an internal redirect :
http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VFKqiYXXqPK
I made several attempts but the redirect is always shown.
As I'm new to Nginx, can somebody help me out ?
best regards,

Resources