Server Fault Asked by dustyburwell on January 5, 2022
Is there a way to get nginx to treat a custom variable the same as it treats the $scheme in a rewrite rule? For instance:
rewrite ^/redirect http://example.com;
# and
rewrite ^/redirect_with_scheme $scheme://example.com;
will both result in a 302 redirect being sent to the client. However, this:
set $real_scheme $scheme;
rewrite ^/redirect_with_real_scheme $real_scheme://example.com;
will result in a server side rewrite to a file that doesn’t necessarily exist and then a 404 to the client. For what it’s worth, this:
set $real_scheme $scheme;
rewrite ^/redirect_with_real_scheme $real_scheme://example.com redirect;
will give the desired result of sending a 302 to the client. Unfortunately, I have a lot of redirects set up this way and I would rather not update all of them with the redirect
directive if it’s not necessary.
If it’s helpful, I’m using Nginx 1.6.2.
The documentation states:
If a replacement string starts with “
http://
”, “https://
”, or “$scheme
”, the processing stops and the redirect is returned to a client.
If you start the URL with some other variable, you will need to express the intention to redirect
explicitly.
Answered by Michael Hampton on January 5, 2022
I cannot reproduce your problem.
I tried the following minimal configuration, every rewrite
working as expected (HTTP 302s):
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen 80;
set $real_scheme $scheme;
rewrite ^/redirect http://localhost;
rewrite ^/redirect_with_scheme $scheme://localhost;
rewrite ^/redirect_with_real_scheme $real_scheme://localhost;
}
}
Could you provide me with a minimal configuration able to demonstrate the faulty rewrite
rules?
Answered by Bernard Rosset on January 5, 2022
2 Asked on February 25, 2021 by oliver-giesen
azure active directory exchange microsoft office 365 permissions shared mailbox
1 Asked on February 24, 2021 by spm
1 Asked on February 23, 2021 by kshitiz-sharma
1 Asked on February 22, 2021 by nona
2 Asked on February 21, 2021 by petey
2 Asked on February 20, 2021
0 Asked on February 20, 2021 by akash-pandey
0 Asked on February 19, 2021 by letmesothat4u
1 Asked on February 19, 2021 by aditya-patawari
1 Asked on February 18, 2021 by jakub-urbanowicz
0 Asked on February 18, 2021 by rusty009
0 Asked on February 18, 2021 by fryfubar
1 Asked on February 17, 2021 by oligofren
1 Asked on February 17, 2021 by hein-gustavsen
1 Asked on February 17, 2021 by eroji
2 Asked on February 17, 2021 by matthew-tranmer
0 Asked on February 17, 2021 by mekswoll
1 Asked on February 16, 2021 by biorod
Get help from others!
Recent Answers
Recent Questions
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP