Nginx regex to get uri minus locationhow to remove location block from $uri in nginx configuration?How to make nginx reverse proxy let 503 error pages pass through to client?Laravel 4.1 on nginx routes error 404nginx location regex match everything but homeNginX + WordPress + SSL + non-www + W3TC vhost config file questionsnginx location based on uri pathnginx PHP files downloading instead of executingsimple non-regex nginx location mappingCodeIgniter nginx rewrite rules for i8ln URL'sWhy Nginx calls for invalid certificate in non-existent subdomains just to redirect to 404?NGINX virtual host config for Magento2 in a subfolder
Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)
Convert seconds to minutes
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
In the UK, is it possible to get a referendum by a court decision?
What is a Samsaran Word™?
files created then deleted at every second in tmp directory
Can a virus destroy the BIOS of a modern computer?
Implication of namely
Car headlights in a world without electricity
Are British MPs missing the point, with these 'Indicative Votes'?
How badly should I try to prevent a user from XSSing themselves?
How can I prove that a state of equilibrium is unstable?
Should I tell management that I intend to leave due to bad software development practices?
Did 'Cinema Songs' exist during Hiranyakshipu's time?
Rotate ASCII Art by 45 Degrees
How do conventional missiles fly?
Does the Idaho Potato Commission associate potato skins with healthy eating?
How to calculate the right interval for a timelapse on a boat
When handwriting 黄 (huáng; yellow) is it incorrect to have a disconnected 草 (cǎo; grass) radical on top?
How could sorcerers who are able to produce/manipulate almost all forms of energy communicate over large distances?
Why do I get negative height?
Why are UK visa biometrics appointments suspended at USCIS Application Support Centers?
Send out email when Apex Queueable fails and test it
Finding the reason behind the value of the integral.
Nginx regex to get uri minus location
how to remove location block from $uri in nginx configuration?How to make nginx reverse proxy let 503 error pages pass through to client?Laravel 4.1 on nginx routes error 404nginx location regex match everything but homeNginX + WordPress + SSL + non-www + W3TC vhost config file questionsnginx location based on uri pathnginx PHP files downloading instead of executingsimple non-regex nginx location mappingCodeIgniter nginx rewrite rules for i8ln URL'sWhy Nginx calls for invalid certificate in non-existent subdomains just to redirect to 404?NGINX virtual host config for Magento2 in a subfolder
I have Nginx running as a reverse proxy to a couple applications. One location directive is running correctly, sending requests to a unix socket file and onwards to its upstream wsgi app. The directive I'm having a problem with is location ~ ^/sub/alarm(.*)$. I have a couple of rewrites which seem to be working, but in case they are colliding with my other intentions, I'll explain my intention with each directive:
- The first server directive should redirect all http to https. This seems to work fine.
- The second server directive has one location directive that directs traffic to my wsgi application. This works fine. The other location directive I meant to use to serve static content from
/home/myuser/alarm.example.com/when a GET is received forexample.net/sub/alarm. (e.g. example.net/sub/alarm/pretty.css should hand over /home/myuser/alarm.example.com/pretty.css) Instead the wsgi app is loaded. - The last server directive should redirect alarm.example.net to example.net/sub/alarm since I don't have a wildcard certificate but wanted but an easy shortcut and encryption. This seems to work fine.
conf:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
charset utf-8;
client_max_body_size 75M;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
location ~ ^/sub/alarm(.*)$
alias /home/appusername/alarm.example.com;
index index.html;
try_files $1 $1/;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
I looked at how to remove location block from $uri in nginx configuration? to try to get the part of my location file after the uri. I think I'm missing something about priorities.
Another attempt was without regex:
location /sub/alarm/
alias /home/appusername/alarm.example.com;
index index.html;
try_files $uri $uri/index.html =404;
In the above case I was able to load index.html when going to alarm.example.com (which correctly redirected to https://example.com/sub/alarm/), but all the resources were throwing a 404.
Finally I tried to combine both attempts, but it seems I can't put the tilde inside the location block ('unknown directive' when reloading Nginx):
location /sub/alarm/
~ ^/sub/alarm(.)$
try_files /home/appusername/alarm.example.com$1 /home/appusername/alarm.example.com$1/;
Additional Notes
- The dynamic app on example.com is totally unrelated to the "alarm" app which is static. It's included only because it's getting served instead of the alarm app when I attempt the regex.
- I've always avoided learning anything about regex (probably unwise, but I never really needed it over the last 7 years till today) and am paying the price now that I'm configuring Nginx. I used Regex 101 to get my regex string of
^/sub/alarm(.*)$. It seemed to indicate that I needed to use escape slashes, but Nginx doesn't seem to show that in examples. Please let me know if there's another concept I need to study. I officially end my regex avoidance stance starting today. - If the syntax was valid enough for Nginx to reload, my error was
2015/10/12 20:25:57 [notice] 30500#0: signal process startedin all attempts.
nginx regex
bumped to the homepage by Community♦ yesterday
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 have Nginx running as a reverse proxy to a couple applications. One location directive is running correctly, sending requests to a unix socket file and onwards to its upstream wsgi app. The directive I'm having a problem with is location ~ ^/sub/alarm(.*)$. I have a couple of rewrites which seem to be working, but in case they are colliding with my other intentions, I'll explain my intention with each directive:
- The first server directive should redirect all http to https. This seems to work fine.
- The second server directive has one location directive that directs traffic to my wsgi application. This works fine. The other location directive I meant to use to serve static content from
/home/myuser/alarm.example.com/when a GET is received forexample.net/sub/alarm. (e.g. example.net/sub/alarm/pretty.css should hand over /home/myuser/alarm.example.com/pretty.css) Instead the wsgi app is loaded. - The last server directive should redirect alarm.example.net to example.net/sub/alarm since I don't have a wildcard certificate but wanted but an easy shortcut and encryption. This seems to work fine.
conf:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
charset utf-8;
client_max_body_size 75M;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
location ~ ^/sub/alarm(.*)$
alias /home/appusername/alarm.example.com;
index index.html;
try_files $1 $1/;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
I looked at how to remove location block from $uri in nginx configuration? to try to get the part of my location file after the uri. I think I'm missing something about priorities.
Another attempt was without regex:
location /sub/alarm/
alias /home/appusername/alarm.example.com;
index index.html;
try_files $uri $uri/index.html =404;
In the above case I was able to load index.html when going to alarm.example.com (which correctly redirected to https://example.com/sub/alarm/), but all the resources were throwing a 404.
Finally I tried to combine both attempts, but it seems I can't put the tilde inside the location block ('unknown directive' when reloading Nginx):
location /sub/alarm/
~ ^/sub/alarm(.)$
try_files /home/appusername/alarm.example.com$1 /home/appusername/alarm.example.com$1/;
Additional Notes
- The dynamic app on example.com is totally unrelated to the "alarm" app which is static. It's included only because it's getting served instead of the alarm app when I attempt the regex.
- I've always avoided learning anything about regex (probably unwise, but I never really needed it over the last 7 years till today) and am paying the price now that I'm configuring Nginx. I used Regex 101 to get my regex string of
^/sub/alarm(.*)$. It seemed to indicate that I needed to use escape slashes, but Nginx doesn't seem to show that in examples. Please let me know if there's another concept I need to study. I officially end my regex avoidance stance starting today. - If the syntax was valid enough for Nginx to reload, my error was
2015/10/12 20:25:57 [notice] 30500#0: signal process startedin all attempts.
nginx regex
bumped to the homepage by Community♦ yesterday
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 have Nginx running as a reverse proxy to a couple applications. One location directive is running correctly, sending requests to a unix socket file and onwards to its upstream wsgi app. The directive I'm having a problem with is location ~ ^/sub/alarm(.*)$. I have a couple of rewrites which seem to be working, but in case they are colliding with my other intentions, I'll explain my intention with each directive:
- The first server directive should redirect all http to https. This seems to work fine.
- The second server directive has one location directive that directs traffic to my wsgi application. This works fine. The other location directive I meant to use to serve static content from
/home/myuser/alarm.example.com/when a GET is received forexample.net/sub/alarm. (e.g. example.net/sub/alarm/pretty.css should hand over /home/myuser/alarm.example.com/pretty.css) Instead the wsgi app is loaded. - The last server directive should redirect alarm.example.net to example.net/sub/alarm since I don't have a wildcard certificate but wanted but an easy shortcut and encryption. This seems to work fine.
conf:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
charset utf-8;
client_max_body_size 75M;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
location ~ ^/sub/alarm(.*)$
alias /home/appusername/alarm.example.com;
index index.html;
try_files $1 $1/;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
I looked at how to remove location block from $uri in nginx configuration? to try to get the part of my location file after the uri. I think I'm missing something about priorities.
Another attempt was without regex:
location /sub/alarm/
alias /home/appusername/alarm.example.com;
index index.html;
try_files $uri $uri/index.html =404;
In the above case I was able to load index.html when going to alarm.example.com (which correctly redirected to https://example.com/sub/alarm/), but all the resources were throwing a 404.
Finally I tried to combine both attempts, but it seems I can't put the tilde inside the location block ('unknown directive' when reloading Nginx):
location /sub/alarm/
~ ^/sub/alarm(.)$
try_files /home/appusername/alarm.example.com$1 /home/appusername/alarm.example.com$1/;
Additional Notes
- The dynamic app on example.com is totally unrelated to the "alarm" app which is static. It's included only because it's getting served instead of the alarm app when I attempt the regex.
- I've always avoided learning anything about regex (probably unwise, but I never really needed it over the last 7 years till today) and am paying the price now that I'm configuring Nginx. I used Regex 101 to get my regex string of
^/sub/alarm(.*)$. It seemed to indicate that I needed to use escape slashes, but Nginx doesn't seem to show that in examples. Please let me know if there's another concept I need to study. I officially end my regex avoidance stance starting today. - If the syntax was valid enough for Nginx to reload, my error was
2015/10/12 20:25:57 [notice] 30500#0: signal process startedin all attempts.
nginx regex
I have Nginx running as a reverse proxy to a couple applications. One location directive is running correctly, sending requests to a unix socket file and onwards to its upstream wsgi app. The directive I'm having a problem with is location ~ ^/sub/alarm(.*)$. I have a couple of rewrites which seem to be working, but in case they are colliding with my other intentions, I'll explain my intention with each directive:
- The first server directive should redirect all http to https. This seems to work fine.
- The second server directive has one location directive that directs traffic to my wsgi application. This works fine. The other location directive I meant to use to serve static content from
/home/myuser/alarm.example.com/when a GET is received forexample.net/sub/alarm. (e.g. example.net/sub/alarm/pretty.css should hand over /home/myuser/alarm.example.com/pretty.css) Instead the wsgi app is loaded. - The last server directive should redirect alarm.example.net to example.net/sub/alarm since I don't have a wildcard certificate but wanted but an easy shortcut and encryption. This seems to work fine.
conf:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
charset utf-8;
client_max_body_size 75M;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
location ~ ^/sub/alarm(.*)$
alias /home/appusername/alarm.example.com;
index index.html;
try_files $1 $1/;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
I looked at how to remove location block from $uri in nginx configuration? to try to get the part of my location file after the uri. I think I'm missing something about priorities.
Another attempt was without regex:
location /sub/alarm/
alias /home/appusername/alarm.example.com;
index index.html;
try_files $uri $uri/index.html =404;
In the above case I was able to load index.html when going to alarm.example.com (which correctly redirected to https://example.com/sub/alarm/), but all the resources were throwing a 404.
Finally I tried to combine both attempts, but it seems I can't put the tilde inside the location block ('unknown directive' when reloading Nginx):
location /sub/alarm/
~ ^/sub/alarm(.)$
try_files /home/appusername/alarm.example.com$1 /home/appusername/alarm.example.com$1/;
Additional Notes
- The dynamic app on example.com is totally unrelated to the "alarm" app which is static. It's included only because it's getting served instead of the alarm app when I attempt the regex.
- I've always avoided learning anything about regex (probably unwise, but I never really needed it over the last 7 years till today) and am paying the price now that I'm configuring Nginx. I used Regex 101 to get my regex string of
^/sub/alarm(.*)$. It seemed to indicate that I needed to use escape slashes, but Nginx doesn't seem to show that in examples. Please let me know if there's another concept I need to study. I officially end my regex avoidance stance starting today. - If the syntax was valid enough for Nginx to reload, my error was
2015/10/12 20:25:57 [notice] 30500#0: signal process startedin all attempts.
nginx regex
nginx regex
edited Apr 13 '17 at 12:13
Community♦
1
1
asked Oct 12 '15 at 20:11
Palu MacilPalu Macil
1235
1235
bumped to the homepage by Community♦ yesterday
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♦ yesterday
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 |
add a comment |
1 Answer
1
active
oldest
votes
So the user (eh... me) missed something that should have been a glaring clue:
In the above case I was able to load index.html when going to
alarm.example.com (which correctly redirected to
https://example.com/sub/alarm/), but all the resources were throwing a
404.
Even though that example still wasn't correct, I should have checked file permissions on the resource files. Nginx is running as www-data and in the group for the index.html file but needed to be in the group for all files. The file owner is the appusername user.
I've since added another app (called 'beer') which is routed like alarm. I've now learned the basics of regex and was able to do this in one location block:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server beer)(.*)$
alias /home/appusername/$1.example.com/;
#index index.html index.htm;
try_files $2 $2/ =404;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
Don't mind the switch in order for the location blocks. That is only coincidence of typing and retyping.
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%2f728451%2fnginx-regex-to-get-uri-minus-location%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So the user (eh... me) missed something that should have been a glaring clue:
In the above case I was able to load index.html when going to
alarm.example.com (which correctly redirected to
https://example.com/sub/alarm/), but all the resources were throwing a
404.
Even though that example still wasn't correct, I should have checked file permissions on the resource files. Nginx is running as www-data and in the group for the index.html file but needed to be in the group for all files. The file owner is the appusername user.
I've since added another app (called 'beer') which is routed like alarm. I've now learned the basics of regex and was able to do this in one location block:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server beer)(.*)$
alias /home/appusername/$1.example.com/;
#index index.html index.htm;
try_files $2 $2/ =404;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
Don't mind the switch in order for the location blocks. That is only coincidence of typing and retyping.
add a comment |
So the user (eh... me) missed something that should have been a glaring clue:
In the above case I was able to load index.html when going to
alarm.example.com (which correctly redirected to
https://example.com/sub/alarm/), but all the resources were throwing a
404.
Even though that example still wasn't correct, I should have checked file permissions on the resource files. Nginx is running as www-data and in the group for the index.html file but needed to be in the group for all files. The file owner is the appusername user.
I've since added another app (called 'beer') which is routed like alarm. I've now learned the basics of regex and was able to do this in one location block:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server beer)(.*)$
alias /home/appusername/$1.example.com/;
#index index.html index.htm;
try_files $2 $2/ =404;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
Don't mind the switch in order for the location blocks. That is only coincidence of typing and retyping.
add a comment |
So the user (eh... me) missed something that should have been a glaring clue:
In the above case I was able to load index.html when going to
alarm.example.com (which correctly redirected to
https://example.com/sub/alarm/), but all the resources were throwing a
404.
Even though that example still wasn't correct, I should have checked file permissions on the resource files. Nginx is running as www-data and in the group for the index.html file but needed to be in the group for all files. The file owner is the appusername user.
I've since added another app (called 'beer') which is routed like alarm. I've now learned the basics of regex and was able to do this in one location block:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server beer)(.*)$
alias /home/appusername/$1.example.com/;
#index index.html index.htm;
try_files $2 $2/ =404;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
Don't mind the switch in order for the location blocks. That is only coincidence of typing and retyping.
So the user (eh... me) missed something that should have been a glaring clue:
In the above case I was able to load index.html when going to
alarm.example.com (which correctly redirected to
https://example.com/sub/alarm/), but all the resources were throwing a
404.
Even though that example still wasn't correct, I should have checked file permissions on the resource files. Nginx is running as www-data and in the group for the index.html file but needed to be in the group for all files. The file owner is the appusername user.
I've since added another app (called 'beer') which is routed like alarm. I've now learned the basics of regex and was able to do this in one location block:
server
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
server beer)(.*)$
alias /home/appusername/$1.example.com/;
#index index.html index.htm;
try_files $2 $2/ =404;
location /
include uwsgi_params;
uwsgi_pass unix:///tmp/example.com.sock;
server
listen 80;
server_name alarm.example.com;
rewrite ^ $scheme://example.com/sub/alarm$request_uri permanent;
Don't mind the switch in order for the location blocks. That is only coincidence of typing and retyping.
answered Oct 13 '15 at 18:20
Palu MacilPalu Macil
1235
1235
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%2f728451%2fnginx-regex-to-get-uri-minus-location%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