The following configuration will rewrite /admin while proxy_pass /core, I can't figure out the reason. Any hint on this? Similar case like this Nginx: location regex for multiple paths with backend .
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /\#$1 break;
}
}
location ~ ^/(admin|core)/ {
proxy_pass http://127.0.0.1:8080/$1;
}
Using try_files will simplify things.
location /
try_files $uri $uri/ =404;
}
location ~ ^/(?:admin|core)/ {
proxy_pass http://127.0.0.1:8080;
}
Try this variant:
location ~ ^/(admin|core)(.*)$ {
proxy_pass http://127.0.0.1:8080/$1;
}
Related
what i want to do is reverse proxy b.com/c/d/1.jpg directory with a.com/1.jpg
location / {
proxy_ssl_server_name on;
if (!-e $request_filename) {
# below will cause error: cannot have URI part in location given by regular expression, or inside named location, or inside \"if\" statement,
# proxy_pass https://b.com/c/d;
proxy_pass https://b.com;
}
}
can anyone help? how to do this on right way. THX.
Replace if (!-e $request_filename) with a try_files statement, and place the proxy_pass into a named location. Use rewrite...break to adjust the URI before it's passed upstream.
For example:
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
rewrite ^/(.*)$ /c/d/$1 break;
proxy_ssl_server_name on;
proxy_pass https://b.com;
}
I have a some case to map in nginx
for / it take from the /var/www/html/cont
for /content/* it take form the var/www/html/cont
if it is not / and not /content/*
the it should take from /var/www/html/web
i am stucking in case 3
Here is my config
location / {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
Any help appreciated.
Thank you
Set root for server {} context as root /var/www/html/web;
then
location / {
root /var/www/html/cont;
# your other directives
}
location /content {
root /var/www/html/cont;
# your other directives
}
Here is my solution which works perfectly in as per my use case.
location / {
if ($request_uri = "/") {
set $custom_root /var/www/html/cont;
}
if ($request_uri != "/") {
set $custom_root /var/www/html/web;
}
root $custom_root;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
}
I want to do redirect from urls, real page exist here - mysite.com/folder/index.html :
mysite.com/folder/index.html to mysite.com/folder/
mysite.com/folder.html to mysite.com/folder/
mysite.com/index.html to mysite.com
This is part of my config
server_name k.my.net www.k.my.net;
index index.html;
root /var/www/demo/k.my.net/current/public;
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1/;
location / {
try_files $uri $uri/ =404;
}
Also try do it with:
location / {
try_files $uri $uri/ #htmlext;
}
location ~ \.html$ {
try_files $uri =404;
}
location #htmlext {
rewrite ^(.*)$ $1.html permanent;
}
3-rd solution ERROR_LOOP
location ~* ^/([a-zA-Z1-9_-]*/)index\.html$ {
return 301 $1;
}
location ~* ^/([a-zA-Z1-9_-]*/?[1-9a-zA-Z_-]*)\.html$ {
return 301 /$1/;
}
location ~* ^/([a-zA-Z1-9_-]*/?[a-zA-Z1-9_-]*)/$ {
try_files /$1.html /$1/index.html =404;
}
You could use a regular expression location to extract the part of the URI before the trailing / and use try_files to test the two alternatives. See this document for details.
For example:
location ~ ^(.*)/$ {
try_files $1/index.html $1.html =404;
}
The location also matches / which will satisfy your third requirement.
Your rewrite statements should be safe, but if they cause redirection loops, you may need to replace them with an if block and test the original request in $request_uri. For example:
if ($request_uri ~ ^([^?]*?)(/index|)(\.html)(\?.*)?$) {
return 301 $1/$4;
}
location = /land {
proxy_pass http://172.0.0.78:3033;
if ($http_user_agent ~* "(Android|iPhone|iPod|Symbian|BlackBerry|Windows Phone|Mobile|J2ME)") {
root /home/xxx/;
rewrite /(.*) /index.html;
break;
}
}
Excuse me, where is the above problem?
How should I modify it?
thks~
It's always wise to separate proxied and static content:
location = /land {
if ($http_user_agent ~* "(Android|iPhone|iPod|Symbian|BlackBerry|Windows Phone|Mobile|J2ME)") {
rewrite ^ /index.html last;
}
proxy_pass http://172.0.0.78:3033;
}
location = /index.html {
root /home/xxx/;
}
I'm running into small hick up with try_files in combination with proxy_pass (or alias for that matter).
Current config:
location /staticsrv {
alias /var/www/static/trunk/;
#proxy_pass http://static.localtest.nl/;
}
location ~ ^/staticsrv/images/gallery/(.*)$ {
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^(.*)$ /index.php/?c=media&m=index&imag=$uri;
}
However for every file it gets dropped to the rewrite rule as it doesn't exist.
Is there a "trick" (read correct config) to fix my misfortune? Or is it just not possible? Both domains will eventually be on the same server so we can work with alias and proxy_pass.
Thanks in advance
Your location ~ ^/staticsrv/images/gallery/(.*)$ needs a root or an alias to construct a local path for try_files to try. Also, you do not necessarily need a regular expression here:
location /staticsrv/images/gallery/ {
alias /var/www/static/trunk/images/gallery/;
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
}
proxy_pass will not work with try_files as one deals with remote content and the other with local content.
I try to avoid using alias and try_files in the same location block because of this open bug.
A possible work around would be to use another intermediate URI that closely matches the document root:
location /staticsrv/images/gallery/ {
rewrite ^/staticsrv(.+)$ /trunk$1 last;
}
location /trunk {
internal;
root /var/www/static;
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
}