Wrong with my nginx rewrite rule - nginx

My first rewrite rule is :
location / {
root E://w/q/__t/q/;
index index.html index.htm;
}
then I request 127.0.0.1/test.js
I can fetch the test.js file in the fold E://w/q/__t/q/
then I update the rewrite rule, I add a /js/ path both in my location and request path:
location /js/ {
root E://w/q/__t/q/;
index index.html index.htm;
}
then I request 127.0.0.1/js/test.js
but the nginx return 404
So what's wrong with my code? How can make it correct?
My nginx version is 1.5.8 and my OS is Windows 7

well, you don't actually use the rewrite command!
With this config, Nginx will look at E://w/q/__t/q/js/test.js when you request 127.0.0.1/js/test.js
So copy your js there, or use a rewrite command to remove the js part in your url.

You didn't update the root, you can either do that or use alias instead of root
location /js/ {
root E://w/q/__t/q/js;
index index.html index.htm;
}
or
location /js/ {
alias E://w/q/__t/q/;
index index.html index.htm;
}
Ps: try to avoid placing index and root inside locations, it's a bad practice, also if you're going to use alias make sure not to use try_files with it

Related

nginx serve index file from subfolders

I would like to serve an index file from a root folder and one from a subfolder from root.
My nginx server conf looks as follows:
server {
listen 80;
server_name localhost;
root /data/;
index index.html index.htm index.txt;
location / {
}
location /a {
alias /data/a/;
}
}
and my directory structure looks as follows:
/data/:
a index.txt
/data/a:
index.txt
If I then do curl localhost, I get the contents of the file /data/index.txt.
But curl /localhost/a gives me a 301.
curl localhost/a/index.txt works.
Why can't I access my index.txt with curl /localhost/a ?
I tried using a root instead of alias in the location /a block and also tried to specify the index.txt for location /a, but no success.
I see similar posts, e.g.
Nginx location configuration (subfolders)
but couldn't yet find the answer.
The index directive works with URIs that end with a /:
So the URI / gives you the contents of /index.txt and /a/ gives you the contents of `/a/index.txt.
If you provide Nginx with a URI of a directory (but without a trailing /), the default behaviour is to redirect to the same URI, but with a trailing /.
This is just how the index directive works. See this document for details.
If you want something other than default behaviour you will have to do it manually using try_files. See this document for details.
For example, to return the contents of an index.txt file by providing the URI of the directory without a trailing /, use:
root /data;
location / {
try_files $uri $uri/index.txt =404;
}
Note that the location /a { ... } block is not necessary in either this example, or the example in your question.

nginx config with spa and subdirectory root

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.

nginx redirect to index.html file

I have just started using nginx and would like to know how can I point:
https://xxx.yyy.zzz/abc/test/
to
https://xxx.yyy.zzz/abc/test/index.html
Any ideas?
location /abc/test {
root /local/path/to/html;
index index.html index.htm;
}

Nginx root location

On our server we have the /var/www/root/html/web/ directory that contains all the code, and a /var/www/root/html/web/front/ that contains our static frontend code.
Our frontend communicates with the code only via REST API calls, which have the /api/ prefix, so all the calls will be accessible via ourdomain.com/api/products/ , ourdomain.com/api/products/45 and so on. We also have an admin running there, on ourdomain.com/admin
When we want to see the actual frontend, we have to go to ourdomain.com/front in the browser, which is of course not what we want.
We have, among other stuff, this in our config:
root /var/www/html/web;
index index.php index.html;
location /front {
# some magic to make sure the /front folder will not be parsed
index nothing_will_match;
autoindex on;
}
However, what we wish is that if you go to ourdomain.com it will load /var/www/html/web/front/ folder as root, and if you go to ourdomain.com/api/* or ourdomain.com/admin/* it will load the /var/www/html/web/ as root. Is that possible?
NOTE: the /var/www/html/web/front/ folder can be moved somewhere else if needed, to /var/www/html/front/ for example
What you need is the alias directive (instead of root) to rewrite the URI:
root /var/www/html/web/front;
index index.php index.html;
location / {
}
location /api {
alias /var/www/html/web/;
}
location /admin {
alias /var/www/html/web/;
}

Removing index extension in nginx

With Apache the directive DirectoryIndex index along with DefaultType application/x-httpd-php within a particular vhost worked quite well to exclude a file extension from index files without rewriting. How can I duplicate this in Nginx? So far all I've been able to find is regex rewriting solutions.
The .conf file would look something like this:
server {
server_name example.com;
# Set the docroot directly in the server
root /var/www;
# Allow index.php or index.html as directory index files
index index;
# See if a file or directory was requested first. If not, try the request as a php file.
location / {
try_files $uri $uri/;
}
}
the line try_files $uri should try the files without extensions on the backend

Resources