How to rewrite the URL using nginx - nginx

I have a URL as
https://test.rockon.me/Profiles/XYZ-ABC-PQRS/default.aspx
now using nginx i have to write rules for creating a subdomain which can make the URL as https://XYZ-ABC-PQRS/test.rockon.me/Profiles/default.aspx here XYZ-ABC-PQRS is the username of some user.
server
{
access_log /var/log/nginx/subcalls.log;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;
listen 80;
server_name ~^(?<subdomain>.+)\nithinveer\.com$;
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain$request_uri/;
}

Yours not working, probably because you expect $request_uri = /default.aspx which is not, it is actually, everything after subdomain i.e., /Profiles/user/default.aspx
Try this,
server {
server_name test.rockon.me;
rewrite ^/Profiles/(.*)/default.aspx http://$1/test.rockon.me/Profiles/default.aspx permanent;
}
The other option using proxy_pass can be,
server{
server_name test.rockon.me;
location / {
rewrite /Profiles/(.*)/(.*) /Profiles/$1/$2 break;
proxy_pass http://192.168.6.190;
}
}
Hope it helps. :)

Related

Regex in nginx inside map

I cannot fix routing in nginx for different parts in URI . So if the request has URI starting with de it should pass the traffic to app_b. That does not happen and I'm getting error: invalid URL prefix in "http://"
Here's the config.
map $request_uri $resources_location {
"/" "app_a:1234/";
"^.*de.*$" "app_b:2345/";
}
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$resources_location;
proxy_redirect off;
}
location ~ ^/(assets|public|favicon.ico) {
proxy_pass http://$resources_location;
}
}
How that can be solved (also with a help of map)?

Permanent redirect while reverse proxying with nginx

I have setup an nginx reverse proxy server which proxy blog.xxx.com to xxx.com/blog. Here is my config file.
server {
listen 80;
root /var/www/html;
server_name xxx.com www.xxx.com;
location /.well-known/acme-challenge {
root /tmp/letsencrypt/www;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443;
server_name xxx.com;
root /var/www/html;
include /etc/nginx/snippet/ssl.conf;
location /blog/ {
proxy_pass https://blog.xxx.com;
proxy_set_header Host blog.xxx.com;
rewrite /blog/(.*) /$1 break;
proxy_redirect off;
expires -1;
add_header Cache-Control no-store;
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://xxx:8090;
}
}
It works just fine. But I need to have a permanent redirect on blog.xxx.com to xxx.com/blog as well. Once I set the redirect rule, a too many redirects situation happens.
Is there any way to have both reverse proxy and 301 redirect at the same time?
Do I understand correctly that the two sites mentioned are hosted in different places? If so, I would do a 303 redirect to your second page on the first page. If you don't have Nginx on the first page, you can probably do this in your blog software (or directly in HTML, PHP, etc.) To prevent endless redirection, you can rewrite them while sending them to the client:
https://serverfault.com/a/986034/304842

Nginx proxy_pass with rewrite in url?

I would like to implement a reverse proxy which redirect request of http://www.dummy.com/foo/bar/test to http://127.0.0.1/hello/world. I have tried to add rewrite before the pass and it seems not working ...
server {
listen 80;
listen [::]:80;
server_name www.dummy.com;
# access_log /var/log/nginx/upstream_log.log
location / {
root /usr/share/nginx/html/dummy;
}
location /foo/bar/test {
rewrite ^/foo/bar/test /hello/world break;
proxy_pass http://127.0.0.1/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
access_log /var/log/nginx/upstream_log.log upstream_logging;
}
}
Is there something missing or wrongly configured?
The above config works as expected... The other server was misconfigured when I test the above configuration.

Redirecting a URL using nginx proxy

I have the following configuration in a linux nginx reverse proxy setup. All is working fine for the basic redirection. What I want to achieve is a rewrite from webmail.someserver.net to webmail.someserver.net/gw
I can't seem to get any rewrite rule to achieve this successfully and I'm probably missing something very simple. Can anyone provide any ideas?
Thanks
server {
listen 80;
server_name webmail.someserver.net;
access_log /var/log/www/webmail.someserver.net.access.log;
error_log /var/log/www/webmail.someserver.net.error.log error;
rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https
index index.html index.htm;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/servercerts/someserver.net.chained.crt; # path to your cacert.pem
ssl_certificate_key /etc/ssl/servercerts/certified_wildcard_certificate_Private.key; # path to your privkey.pem
server_name webmail.someserver.net; #Domain Name
proxy_set_header X-Forwarded-For $remote_addr;
# rewrite ^ $scheme://$host/gw redirect;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
server_tokens off;
location / {
proxy_pass https://ebvl-009.cdn.someserver.net;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
If you simply want to redirect / to /gw use:
location = / {
return 301 /gw;
}
See this and this for more details.
As an aside, your statement rewrite ^ https://$http_host$request_uri? permanent; can also be written more simply as: return 301 https://$http_host$request_uri

nginx not stripping www using rewrite

I want to strip the www from my url in my nginx configuration and looking around the documentation and stack overflow posts I wrote the following configuration but it doesn't seem to be working.
server_name {
server_name www.subdomain.domain.com;
rewrite ^(.*) https://subdomain.domain.com/$1 permanent
}
server_name {
server_name subdomain.domain.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /file;
index index.html index.htm app.js;
location /{
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /*/ {
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
}
But it doesn't seem to be working at all. However whenever I try to go to the site using www.subdomain.domain.com i can't access the site but doing https://subdomain.domain.com works fine. Any advice on this would be great thanks.
You have typo in nginx config (server_name instead server), and try return instead rewrite as more proper way:
server {
server_name www.subdomain.domain.com;
return 301 $scheme://subdomain.domain.com$request_uri;
}

Resources