nginx rewrite url without changing browser addressIn Nginx, how can I rewrite all http requests to https...
Finding the number of integers that are a square and a cube at the same time
Can the Count of Monte Cristo's calculation of poison dosage be explained?
How can I mix up weapons for large groups of similar monsters/characters?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
What is better: yes / no radio, or simple checkbox?
Do any poskim exempt 13-20-year-olds from Mussaf?
What's the purpose of these copper coils with resitors inside them in A Yamaha RX-V396RDS amplifier?
LTSpice: When running a linear AC simulation, how to view the voltage ratio between two voltages?
How should I state my MS degree in my CV when it was in practice a joint-program?
Why didn't Eru and/or the Valar intervene when Sauron corrupted Númenor?
Can a person refuse a presidential pardon?
Can I retract my name from an already published manuscript?
How to approximate rolls for potions of healing using only d6's?
How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?
How to properly claim credit for peer review?
Predict mars robot position
What's the rationale behind the objections to these measures against human trafficking?
Am I a Rude Number?
What happens if a wizard reaches level 20 but has no 3rd-level spells that they can use with the Signature Spells feature?
Why is this code uniquely decodable?
When does coming up with an idea constitute sufficient contribution for authorship?
ip vs ifconfig commands pros and cons
Where was Karl Mordo in Infinity War?
I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
nginx rewrite url without changing browser address
In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?nginx reverse proxyTrouble with nginx and serving from multiple directories under the same domainNginx + Apache + Wordpress redirects to localhost/127.0.0.1Nginx load balancing redirects to upstream “name”nginx rewrite throw 404 with last and breaknginx reverse proxy hide login query also on 301 redirect or full qualified urlCodeIgniter nginx rewrite rules for i8ln URL'sUniversal HTTPS to HTTP reverse proxy using nginxnginx rewrite without url decoding
I want to change the url of a request but the browser address should not change.
To achieve this I have tried the below configuration:
location /my-website {
proxy_pass http://tomcat/my-website;
}
location =/my-website {
rewrite /my-website$(.*) $1/my-website/mypage/index.html last;
}
Although doing this the request does get the correct address but the address bar of the browser also changes.
Also tried;
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website$(.*) $1/my-website/page/index.html break;
}
Any suggestion(s) on improving this configuration?
Expected Output
address bar: protocol://localhost/my-website
actual: protocol://localhost/my-website/page
Current Output
address bar: protocol://localhost/my-website/page
actual: protocol://localhost/my-website/page
Things tried :
- https://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.W3_a6M4zaUk
- https://stackoverflow.com/questions/15322826/nginx-rewrite-without-change-url
Edit
The above issue is being faced on a 302 redirect. For other cases the url is changed without changing the browser address. I am handling the latter case using the below config:
location /my-website {
proxy_pass http://tomcat;
rewrite ^(.*)my-website/src(.*)$ $1my-website/page/src$2 break;
}
i.e. that location is followed by /src and it works.
In the 302 case the location is just my-website/ and the above tried things fail.
My file config:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_location"';
rewrite_log on;
#log_format graylog2_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" <msec=$msec|connection=$connection|connection_requests=$connection_requests|millis=$request_time>';
error_log logs/error.log warn;
sendfile on;
keepalive_timeout 65;
map $http_user_agent $ua_redirect {
default 'abc';
}
upstream docker-mysite {
server localhost:9012;
}
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
client_max_body_size 0;
server {
listen 80;
access_log logs/host.access.log main;
#below config works
location /mysite {
proxy_pass http://docker-mysite;
rewrite ^(.*)mysite/src(.*)$ $1mysite/$ua_redirect/src$2 break;
}
#below config works but modifies the browser url hence the issue
location = /mysite {
proxy_pass http://docker-mysite;
rewrite /mysite(.*)$ /mysite/$ua_redirect$1 break;
}
}
}
Logs
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/login?code=token HTTP/1.1" 302 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"http://localhost/mysite/abc
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/abc HTTP/1.1" 404 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"-
nginx mod-rewrite rewrite url
bumped to the homepage by Community♦ 12 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I want to change the url of a request but the browser address should not change.
To achieve this I have tried the below configuration:
location /my-website {
proxy_pass http://tomcat/my-website;
}
location =/my-website {
rewrite /my-website$(.*) $1/my-website/mypage/index.html last;
}
Although doing this the request does get the correct address but the address bar of the browser also changes.
Also tried;
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website$(.*) $1/my-website/page/index.html break;
}
Any suggestion(s) on improving this configuration?
Expected Output
address bar: protocol://localhost/my-website
actual: protocol://localhost/my-website/page
Current Output
address bar: protocol://localhost/my-website/page
actual: protocol://localhost/my-website/page
Things tried :
- https://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.W3_a6M4zaUk
- https://stackoverflow.com/questions/15322826/nginx-rewrite-without-change-url
Edit
The above issue is being faced on a 302 redirect. For other cases the url is changed without changing the browser address. I am handling the latter case using the below config:
location /my-website {
proxy_pass http://tomcat;
rewrite ^(.*)my-website/src(.*)$ $1my-website/page/src$2 break;
}
i.e. that location is followed by /src and it works.
In the 302 case the location is just my-website/ and the above tried things fail.
My file config:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_location"';
rewrite_log on;
#log_format graylog2_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" <msec=$msec|connection=$connection|connection_requests=$connection_requests|millis=$request_time>';
error_log logs/error.log warn;
sendfile on;
keepalive_timeout 65;
map $http_user_agent $ua_redirect {
default 'abc';
}
upstream docker-mysite {
server localhost:9012;
}
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
client_max_body_size 0;
server {
listen 80;
access_log logs/host.access.log main;
#below config works
location /mysite {
proxy_pass http://docker-mysite;
rewrite ^(.*)mysite/src(.*)$ $1mysite/$ua_redirect/src$2 break;
}
#below config works but modifies the browser url hence the issue
location = /mysite {
proxy_pass http://docker-mysite;
rewrite /mysite(.*)$ /mysite/$ua_redirect$1 break;
}
}
}
Logs
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/login?code=token HTTP/1.1" 302 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"http://localhost/mysite/abc
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/abc HTTP/1.1" 404 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"-
nginx mod-rewrite rewrite url
bumped to the homepage by Community♦ 12 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16
add a comment |
I want to change the url of a request but the browser address should not change.
To achieve this I have tried the below configuration:
location /my-website {
proxy_pass http://tomcat/my-website;
}
location =/my-website {
rewrite /my-website$(.*) $1/my-website/mypage/index.html last;
}
Although doing this the request does get the correct address but the address bar of the browser also changes.
Also tried;
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website$(.*) $1/my-website/page/index.html break;
}
Any suggestion(s) on improving this configuration?
Expected Output
address bar: protocol://localhost/my-website
actual: protocol://localhost/my-website/page
Current Output
address bar: protocol://localhost/my-website/page
actual: protocol://localhost/my-website/page
Things tried :
- https://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.W3_a6M4zaUk
- https://stackoverflow.com/questions/15322826/nginx-rewrite-without-change-url
Edit
The above issue is being faced on a 302 redirect. For other cases the url is changed without changing the browser address. I am handling the latter case using the below config:
location /my-website {
proxy_pass http://tomcat;
rewrite ^(.*)my-website/src(.*)$ $1my-website/page/src$2 break;
}
i.e. that location is followed by /src and it works.
In the 302 case the location is just my-website/ and the above tried things fail.
My file config:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_location"';
rewrite_log on;
#log_format graylog2_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" <msec=$msec|connection=$connection|connection_requests=$connection_requests|millis=$request_time>';
error_log logs/error.log warn;
sendfile on;
keepalive_timeout 65;
map $http_user_agent $ua_redirect {
default 'abc';
}
upstream docker-mysite {
server localhost:9012;
}
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
client_max_body_size 0;
server {
listen 80;
access_log logs/host.access.log main;
#below config works
location /mysite {
proxy_pass http://docker-mysite;
rewrite ^(.*)mysite/src(.*)$ $1mysite/$ua_redirect/src$2 break;
}
#below config works but modifies the browser url hence the issue
location = /mysite {
proxy_pass http://docker-mysite;
rewrite /mysite(.*)$ /mysite/$ua_redirect$1 break;
}
}
}
Logs
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/login?code=token HTTP/1.1" 302 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"http://localhost/mysite/abc
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/abc HTTP/1.1" 404 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"-
nginx mod-rewrite rewrite url
I want to change the url of a request but the browser address should not change.
To achieve this I have tried the below configuration:
location /my-website {
proxy_pass http://tomcat/my-website;
}
location =/my-website {
rewrite /my-website$(.*) $1/my-website/mypage/index.html last;
}
Although doing this the request does get the correct address but the address bar of the browser also changes.
Also tried;
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website$(.*) $1/my-website/page/index.html break;
}
Any suggestion(s) on improving this configuration?
Expected Output
address bar: protocol://localhost/my-website
actual: protocol://localhost/my-website/page
Current Output
address bar: protocol://localhost/my-website/page
actual: protocol://localhost/my-website/page
Things tried :
- https://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.W3_a6M4zaUk
- https://stackoverflow.com/questions/15322826/nginx-rewrite-without-change-url
Edit
The above issue is being faced on a 302 redirect. For other cases the url is changed without changing the browser address. I am handling the latter case using the below config:
location /my-website {
proxy_pass http://tomcat;
rewrite ^(.*)my-website/src(.*)$ $1my-website/page/src$2 break;
}
i.e. that location is followed by /src and it works.
In the 302 case the location is just my-website/ and the above tried things fail.
My file config:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_location"';
rewrite_log on;
#log_format graylog2_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" <msec=$msec|connection=$connection|connection_requests=$connection_requests|millis=$request_time>';
error_log logs/error.log warn;
sendfile on;
keepalive_timeout 65;
map $http_user_agent $ua_redirect {
default 'abc';
}
upstream docker-mysite {
server localhost:9012;
}
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
client_max_body_size 0;
server {
listen 80;
access_log logs/host.access.log main;
#below config works
location /mysite {
proxy_pass http://docker-mysite;
rewrite ^(.*)mysite/src(.*)$ $1mysite/$ua_redirect/src$2 break;
}
#below config works but modifies the browser url hence the issue
location = /mysite {
proxy_pass http://docker-mysite;
rewrite /mysite(.*)$ /mysite/$ua_redirect$1 break;
}
}
}
Logs
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/login?code=token HTTP/1.1" 302 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"http://localhost/mysite/abc
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/abc HTTP/1.1" 404 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"-
nginx mod-rewrite rewrite url
nginx mod-rewrite rewrite url
edited Sep 3 '18 at 6:59
Syed Osama Maruf
asked Aug 24 '18 at 10:55
Syed Osama MarufSyed Osama Maruf
88
88
bumped to the homepage by Community♦ 12 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 12 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16
add a comment |
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16
add a comment |
2 Answers
2
active
oldest
votes
Try this:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
I think you have a $
out of place in your regexp - it should be at the end of the regexp. I can't tell exactly what your intentions are but you might also want to change your rewrite target, so that pages other than index.html
will work:
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}
1
To extend on this answer: Your rewrite ofrewrite /my-website$(.*)
is basically saying that the URL ends at/my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com
– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
add a comment |
Your rewrite logic is fine since its working correctly in normal schenerio. Browser URL is changing for 302 redirect because rewrite
directive just changes the request URI, not the response of request.
A typical 302 reponse looks like this:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
header in 302 response forces the browser to follow mentioned url.
Possible solution is to modify Location
header from the proxied response (in case of 302 redirect) using proxy_redirect
directive
Helpful links:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "2"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f927820%2fnginx-rewrite-url-without-changing-browser-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
I think you have a $
out of place in your regexp - it should be at the end of the regexp. I can't tell exactly what your intentions are but you might also want to change your rewrite target, so that pages other than index.html
will work:
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}
1
To extend on this answer: Your rewrite ofrewrite /my-website$(.*)
is basically saying that the URL ends at/my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com
– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
add a comment |
Try this:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
I think you have a $
out of place in your regexp - it should be at the end of the regexp. I can't tell exactly what your intentions are but you might also want to change your rewrite target, so that pages other than index.html
will work:
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}
1
To extend on this answer: Your rewrite ofrewrite /my-website$(.*)
is basically saying that the URL ends at/my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com
– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
add a comment |
Try this:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
I think you have a $
out of place in your regexp - it should be at the end of the regexp. I can't tell exactly what your intentions are but you might also want to change your rewrite target, so that pages other than index.html
will work:
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}
Try this:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
I think you have a $
out of place in your regexp - it should be at the end of the regexp. I can't tell exactly what your intentions are but you might also want to change your rewrite target, so that pages other than index.html
will work:
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}
answered Aug 31 '18 at 12:46
dwurfdwurf
858613
858613
1
To extend on this answer: Your rewrite ofrewrite /my-website$(.*)
is basically saying that the URL ends at/my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com
– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
add a comment |
1
To extend on this answer: Your rewrite ofrewrite /my-website$(.*)
is basically saying that the URL ends at/my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com
– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
1
1
To extend on this answer: Your rewrite of
rewrite /my-website$(.*)
is basically saying that the URL ends at /my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com– Rhys
Aug 31 '18 at 14:03
To extend on this answer: Your rewrite of
rewrite /my-website$(.*)
is basically saying that the URL ends at /my-website
as $ is the symbol for end-of-line in PCRE. Nginx uses the PCRE library during compilation. A good place to test out your regex is at regex101.com– Rhys
Aug 31 '18 at 14:03
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
just tried your solution. the url is correctly re written however the browser address is also changed. i don't know if it will help but the re write is to happen on a 302 server redirect. For other cases the following config works fine proxy_pass docker-iam; rewrite ^(.*)iam/src(.*)$ $1iam/$ua_redirect/src$2 break;
– Syed Osama Maruf
Sep 3 '18 at 6:14
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
i will add this to the question as well
– Syed Osama Maruf
Sep 3 '18 at 6:15
add a comment |
Your rewrite logic is fine since its working correctly in normal schenerio. Browser URL is changing for 302 redirect because rewrite
directive just changes the request URI, not the response of request.
A typical 302 reponse looks like this:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
header in 302 response forces the browser to follow mentioned url.
Possible solution is to modify Location
header from the proxied response (in case of 302 redirect) using proxy_redirect
directive
Helpful links:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
add a comment |
Your rewrite logic is fine since its working correctly in normal schenerio. Browser URL is changing for 302 redirect because rewrite
directive just changes the request URI, not the response of request.
A typical 302 reponse looks like this:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
header in 302 response forces the browser to follow mentioned url.
Possible solution is to modify Location
header from the proxied response (in case of 302 redirect) using proxy_redirect
directive
Helpful links:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
add a comment |
Your rewrite logic is fine since its working correctly in normal schenerio. Browser URL is changing for 302 redirect because rewrite
directive just changes the request URI, not the response of request.
A typical 302 reponse looks like this:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
header in 302 response forces the browser to follow mentioned url.
Possible solution is to modify Location
header from the proxied response (in case of 302 redirect) using proxy_redirect
directive
Helpful links:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Your rewrite logic is fine since its working correctly in normal schenerio. Browser URL is changing for 302 redirect because rewrite
directive just changes the request URI, not the response of request.
A typical 302 reponse looks like this:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
header in 302 response forces the browser to follow mentioned url.
Possible solution is to modify Location
header from the proxied response (in case of 302 redirect) using proxy_redirect
directive
Helpful links:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
edited Sep 6 '18 at 19:30
answered Sep 6 '18 at 19:01
Abdul RaufAbdul Rauf
10618
10618
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f927820%2fnginx-rewrite-url-without-changing-browser-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i'm testing with the configuration similar to yours and it work well for me can you add variable $upstream_http_location in nginx access log and share it in here? and maybe give us full config?
– Ilham Sulaksono
Aug 31 '18 at 11:33
@IlhamSulaksono sure i will upload it
– Syed Osama Maruf
Sep 3 '18 at 6:16