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
Related
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.
root directory = /srv/myproject/xyz/main/
in the "main" folder I have few *.html files and I want all of them to point at a url say /test/ (which is quite different from the directory structure)
this is my very basic nginx configuration
server {
listen 80;
error_log /var/log/testc.error.log;
location /test/ {
root /srv/myproject/xyz/main/;
#alias /srv/myproject/xyz/main/;
default_type "text/html";
try_files $uri.html ;
}
}
If I use simple alias
location /test/ {
alias /srv/myproject/xyz/main/;
}
then its work perfectly, I mean I can access those html files by http://www.myurl.com/test/firstfile.html and so on
but I dont want that html extension.
I tried to follow these threads but no success
http://forum.nginx.org/read.php?11,201491,201494
How to remove both .php and .html extensions from url using NGINX?
how to serve html files in nginx without showing the extension in this alias setup
Try this
location ~ ^/test/(.*)$ {
alias /srv/myproject/xyz/main/;
try_files $1.html =404;
}
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
I have 2 issues.
I don't want to require .html file extension for html files
/index => /index.html
I want to serve from user directories
/~username serves from /home/username/www/
I previously used try_files to achieve (1), and I am user the nginx UserDir suggestion:
location ~ /^/~(.+?)(/.*)?$ {
alias /home/$1/www$2;
index index.html index.htm;
autoindex on;
}
The above works for user directories but still requires the .html ext to be used.
I know there is a known bug preventing alias and try_files from working well together.
Thoughts? Sorry if this has been answered before couldn't find a working solution.
You can always replace alias with root
location ~ /^/~([^/]+)(/.*)?$ {
root /home/$1/www;
autoindex on;
try_files $2 $2/ $2.html;
}
PS: move the index to the server scope instead of location
It's a bit old, but as I've been hit by the same problem recently, here is my answer. Thanks to http://marc.info/?l=nginx&m=124533515814122&w=2 I've found that a better answer would be:
location ~ /^/~(.+?)(/.*)?$ {
alias /home/$1/www$2;
index index.html index.htm;
autoindex on;
try_files "" .html / =404;
}
You could add the .html extention to the location regex and the alias:
location ~ /^/~(.+?)(/.*)?.html$ {
alias /home/$1/www$2.html
Note that in this configuration, ONLY html files can be served. You can add another location to support other file extentions.
I'm having a problem with try_files not appearing to pass off requests for non-existent files to the last specified value, in my case index.php. I'm using Wordpress and the XML Sitemap generator plugin I use creates virtual XML files and a virtual robots.txt that's handled by Wordpress. Unfortunately try_files doesn't seem to be passing the requests for these files to Wordpress.
Here's my server configuration:
server {
## Web domain
server_name christiaanconover.com;
## Site root
root /var/www/christiaanconover.com;
## Index
index index.php index.htm;
## Common Wordpress configuration
include wp.conf;
## Include PHP configuration
include php.conf;
## Gzip Compression
include gzip.conf;
## Include W3TC configuration
include /var/www/w3tc/christiaanconover.com;
}
I run multiple separate Wordpress sites on this server, so to save time I created a file wp.conf that contains all the commonly used configuration elements for Wordpress. Here is the contents of wp.conf:
location / {
## Prevent PHP files from being served as static assets, and fall back to index.php if file does not exist
try_files $uri $uri/ /index.php?$args;
## If a file exists, serve it directly
if (-f $request_filename) {
break;
}
## Wordpress Rewrite
if (!-e $request_filename) {
rewrite ^ /index.php last;
}
}
Everything else is working perfectly, but the try_files arrangement just doesn't seem to hand off properly. Any ideas?
you're running into one of the problems described at http://wiki.nginx.org/IfIsEvil#Examples
the ifs you specified in the wp.conf are uneeded you already have them covered with your try_files, so you can just remove them, yielding:
location / {try_files $uri $uri/ /index.php$is_args$args;}
that will:
check if a file matching $uri (relative to the location specified by the root directive) exists
if not check if directory match exists
else redirect to /index.php$is_args$args
Where $is_args evaluates to ? when $args is set.