I'd like to add separated angular app under a specific path (the path should be at the end of an URL to be matched) - I want to keep both versions which are current and a new one but the new should only be available under the specified path. I tried using alias + try_files. My config:
server {
listen 80;
root /dir/project1
server_name ...;
index index.html;
location ~ /path {
alias /dir/project2
try_files $uri $uri/ /index.html;
}
The thing is that when try_files fires up, it takes the path from the root directive - not from the alias. How to fix it? I can only add I cannot use proxy_pass here and root instead of the alias does not work either as it adds paths etc.
The alias directive works differently when placed inside a regular expression location, but you should probably be using a prefix location anyway. See this document for details.
Also, the use of alias and try_files together can cause problems (see this long standing bug).
You are rewriting the URI to /index.html which is the wrong application, and should instead be /path/index.html.
Try:
location ^~ /path {
alias /dir/project2;
if (!-e $request_filename) {
rewrite ^ /path/index.html last;
}
}
See this caution on the use of if.
Related
I'm encountering an annoying error when it comes to my current NGINX app configuration.
I have a static web app which I am indexing on the path /admin/*. I want the index to be available on /admin, with and without a trailing slash, as well as available on a wildcard /admin/* (anything after the trailing slash).
The issue I am facing is that the index is accessable when appending anything after the admin path, for example /adminA/example.
The original NGINX configuration was as follows:
location /admin {
alias /home/user/app/static;
index index.html;
try_files $uri $uri/ /index.html;
}
The best I've been able to implement to stop this at the moment is as follows, however i'm sure it can be done more efficiently:
location = /admin {
alias /home/user/app/static;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin/ {
alias /home/user/app/static/;
index index.html;
try_files $uri $uri/ /admin/index.html;
}
The two location blocks are already efficient, but you could eliminate the redundant code in the first block by redirecting to the second.
Using an internal redirect will be invisible to the browser. For example:
location = /admin {
rewrite ^ /admin/ last;
}
location /admin/ {
...
}
Or use permanent instead of last for an external redirect, which will change the browser's address bar from /admin to /admin/. See the rewrite documentation.
I have received to migrate an existing website written in old php hosted on Apache, and I will deploy to an Nginx.
I wish to have URL like this: http://example.com/about.html
To be executed like this http://example.com/content.php?page=about
So I need to remove leading slash and remove html. The config below works if I hardcode a specific page:
location / {
try_files $uri $uri/ /content.php?page=about;
}
But of course it always serve about regardless if I access our-company.html, or our-services.html. I am not sure what I need to replace the "about" string in the config.
You should use a rewrite directive to perform the actual translation. You can invoke it from a named location specified as the last parameter on the try_files statement.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)\.html$ /content.php?page=$1 last;
}
See this document for more.
I always seem to have problems with nginx configurations. My SPA is located at /mnt/q/app (pushstate is enabled) and the frontend root is located at client/public. Everything should be mapped to index.html, where the app picks up the route and decides what to do.
Full path to the index is /mnt/q/app/client/public/index.html.
I think I ran out of options by now. No matter what I do, I just get a 404 back from nginx, I think the configuration is simple enought and have no clue what's wrong.
server {
listen 80;
server_name app.dev;
root /mnt/q/app;
location / {
root /client/public;
try_files $uri #rewrites =404;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
}
Any help is appreciated.
If nginx views the file system from the root, then the root should be set to /mnt/q/app/client/public, and not either of the two values you are using.
The last element of the try_files directive can be a default action (e.g. /index.html), a named location or a response code. You have a named location in the penultimate element - which will be ignored.
Your named location should work, but is unnecessary, as try_files is capable of implementing it more simply. See this document for more.
For example:
root /mnt/q/app;
location / {
root /mnt/q/app/client/public;
try_files $uri $uri/ /index.html;
}
location /api {
}
location /auth {
}
The $uri/ element will add a trailing / to directories, so that the index directive can work - you do not have to add it if you do not need it.
I already make extensive use of rewrite in nginx to perform this sort of thing:
/photos/123344 -> /photos/photos.php?id=123344
/photos/london-2016 -> /photos/photo-shoot.php?name=london-2016
Currently I have no rule for other (non-dynamic) pages. E.g
/photos/shoot-register.php -> /photos/shoot-register.php
Which I'd like to become
/photos/shoot-register.php -> /photos/shoot-register
But without specifying an individual rewrite rule for each .php file.
It seems that try_files is the correct directive here:
location ~ ^/photos {
try_files $uri $uri.php?$args;
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}
But this doesn't work, unless I delete the two rewrite lines.
I assume that means that execution doesn't stop after try files? It finds "shoot-register.php" but then carries on executing and ends up with /photos/photo-shoot.php?name=shoot-register.php?
How can I make it stop after try_files succeeds in finding a match?
Thanks
what if you move the rewrites to separate named location, and then change your try_file directive to try the file, then the php file and then directing to the new location?
location ~ ^/photos {
try_files $uri $uri.php?$args #rewrites;
}
location #rewrites {
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}
I have a home-made CMS, serving a site which I inherited. I'm not really familiar with nginx rewrite rules, although I could set up tiny URLs. Here is my relevant part of the configuration:
*location / {
index index.php index.html;
root /var/www/www.valami.hu;
try_files $uri $uri/ #seo;
}
location #seo {
rewrite ^/([a-z]+)$ /index.php?oldal=$1 last;
break;
}*
The problem is that the site has a blog which is located on blogspot.com and the stuff from the blog is taken from there. So what I need help with is a rule for this sort of URL:
http://www.valami.hu/index.php?oldal=blog&options=2012/01/some-title-here.html
So, it would be fine like:
http://www.valami.hu/blog/2012/01/some-title-here
The most important is the first rule should be work also as it is more frequently used.
This is actually trivial. Watch and learn!
location / {
try_files $uri $uri/ #site;
}
location #site {
rewrite ^/blog/(.+)$ /index.php?oldal=blog&options=$1 last;
rewrite ^(.+)$ /index.php?oldal=$1 last;
}
The order makes all the difference. You can also do it by removing the last flag and redirecting to /blog with the options query string parameter explicitely set. No if is needed.
well seems we only have 2 cases, the /blog and the non /blog, I'd write 2 location blocks
location ~ ^/blog/(.*) {
try_files $uri /index.php?oldal=blog&options=$1;
}
location ~ /(.*) {
try_files $uri /index.php?oldal=$1;
}
I would have used just / and $request_uri in the second location but that would put a preceeding / in olda1, if that wouldn't matter with you then i'd prefer that method, cause it doesn't involve regex.
About index index.php index.html; and root /var/www/www.valami.hu;, it's better if you move them to the server block instead of the location block, if possible of course.