Redirecting a URL using nginx proxy - nginx

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

Related

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

How to rewrite url /id/slug (WordPress) to just slug (Ghost) in Nginx?

I'm trying to migrate from WordPress to self hosted Ghost blog. In the process I wish to clean up url's for my posts from https://example.com/categoryid/slug to https://example.com/slug. Cateogryid is seems to be whole number containing 1-4 digits.
The problem is that I have also urls that I don't want to rewrite
don't rewrite for img: https://example.com/content/images/2020/01/logo.png
rewrite for post: https://example.com/1886/slug
What I have tried:
this works, but for both url's
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
this should be a match with an online regEx tester, but does not work
rewrite \.*(com)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
passing proxy before and after rewrite rules
location /content/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass example_ip ;
}
full config:
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
# redirect /id/slug -> slug
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
# redirect category -> tag
rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
# redirect blog -> archive
rewrite (blog\/)$ https://example.com/archive permanent;
root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/nginx/snippets/ssl-params.conf;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://example_ip;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
}
In the URLs https://example.com/content/images/2020/01/logo.png and https://example.com/1886/slug, the URI seen by the rewrite directive is /content/images/2020/01/logo.png and /1886/slug respectively.
You need to rewrite URIs which contain 1 to 4 digits in the first path element.
Use either:
rewrite ^/\d+(/.*)$ $1 redirect;
Or:
rewrite "^/\d{1,4}(/.*)$" $1 redirect;
The last variant must use the quotes to protect the embedded brace characters.
See this document for details.

How to rewrite the URL using 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. :)

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;
}

Nginx reverse proxy for tomcat

I am having issues getting my nginx + tomcat 7 reverse proxy setup working.
Basically I want https://192.168.10.101 to serve content from the upstream cluster/webapp/; However I am getting a 404 page from my applicaton.
Any hints on whats going wrong would be greatly appreciated.
My configuration is given below.
server {
server_name 192.168.10.101;
access_log /var/log/nginx/mysite-access.log;
listen 443;
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.crt;
ssl_certificate_key /etc/nginx/ssl/private/mysite_pvt.key;
location / {
proxy_redirect off;
proxy_pass https://tccluster/webapp/;
rewrite_log on;
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_max_temp_file_size 0;
}
}
upstream tccluster {
server 192.168.56.103:8443;
server 192.168.56.104:8443;
}
Finally figured it out. The app has a filter that redirects to /webapp/index.html , which made nginx make the request for /webapp/webapp/index.html which was giving the 404.
I added a rewrite rule
location / {
proxy_pass https://backend/webapp/;
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;
rewrite ^/webapp/(.*)$ /$1 last;
}
And this seems to be working for now !
full nginx config to pass to tomcat context :
server {
listen 80; # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
listen [::]:80;
server_name tomcat-context.domain.com ;
# individual nginx logs for this vhost
access_log /var/log/nginx/tomcat-context_domain_access.log main;
error_log /var/log/nginx/tomcat-context_domain_error.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location / {
proxy_pass http://127.0.0.1:10080/tomcat-context/;
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;
rewrite ^/tomcat-context/(.*)$ /$1 last;
}
location /tomcat-context {
rewrite ^/tomcat-context(.*)$ $1 redirect;
}
}

Resources