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;
}
Related
I am trying to have my nginx instance host from two separate static files.
The config for root path works but using any other prefix pattern gives 404 not found.
The config is as follows:
server {
listen 80;
server_name *.saurabhharwande.com;
root /var/www/certbot;
location /abc/ {
index vindex.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
www.saurabhharwande.com Works
www.saurabhharwande.com/abc Gives 404
Am I missing something?
Edit: My basic understanding of how nginx configuration works was flawed. I thought that index file is searched in the root itself /<domain>/<root>/index.html and location is just used to point to different roots. It is rather searched at <domain>/<root>/<location>/index.html
My root directory
/web/app/src
In this directory I have 2 directories /js/ and /assets/
and one file index.html
This is what I need to achieve:
Any request to /js/ or /assets/ or /index.html just serve files from root directory
For example myapp.com/js/app.js servers app.js from /web/app/src/js/ directory
Same with requests to /assets/
But all other requests, any other uri should result in serving index.html
For example
myapp.com/bla/bla/q?param=x
Should serve index.html from web root directory
All rewrites should be internal, no http 301 redirects.
Sir, please try below code. :)
server {
...
root /web/app/src;
...
location / {
try_files $uri $uri/ /index.html;
}
}
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
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
I have a static file, index.html. How would I configure nginx to serve it from every path on the domain?
URL | file
-----------------
/ | index.html
/foo | index.html
/bar | index.html
/baz | index.html
Essentially, I want a wild card match.
(I realize this will be an unusual setup.)
I faced the same problem a while back and seem to remember doing something along the lines of:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
try_files index.html =404;
}
}
If you don't mind returning an error code (e.g. you're down for maintenance) you could also do something like:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
error 503 index.html;
return 503;
}
}
Is this what you're looking for?
rewrite ^(.*)$ index.html