Nginx - serving assets from user directories - nginx

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.

Related

How do I correctly use try_files when looking in two different directories for files to serve?

I'm quite new to Nginx so I might be misunderstanding of what try_files can do.
For my local development set up I have multiple installations that will each be accesible via their own subdomain. These installations are being migrated into a new folder structure but I still want to have the ability to support both at the same time. When pulled via git the new full path looks like this :
/home/tom/git/project/v3/[installation]/public/
The old structure goes 1 directory deeper namely as follows:
/home/tom/git/project/v3/[installation]/workspace/public
Where installation is variable according to the installation name and the /public folder will be the root for nginx to work from.
The root is determined by the subdomain and is extracted via regex like so:
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
So far I've managed to get all this working for one of the folder structures but not both at the same time. My Nginx configuration for this local domain looks like this. Below is what I've tried but just can't seem to get working. As soon as I pass the #workspace named location as fallback for try_files it always defaults to 404.
index index.html index.htm index.nginx-debian.html index.php;
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
root /home/tom/git/project/v3/$subdomain/public/;
location / {
try_files $uri #workspace =404;
}
location #workspace {
root /home/tom/git/project/v3/$subdomain/workspace/public/;
try_files $uri =404;
}
I have also tried shortening the root and passing the following parameters to try_files
root /home/tom/git/project/v3/$subdomain;
location / {
try_files /public/$uri /workspace/public/$uri =404;
}
But this still defaults to a 404, with a $uri/ as a third parameter there it will emit a 403 forbidden trying to list the directory index of the root.
I hope someone can provide some advice or an alternative as to how to approach this issue I am facing. If I need to provide additional data let me know,
Thanks in advance.
The named location must be the last element of a try_files statement.
For example:
location / {
try_files $uri #workspace;
}
location #workspace {
...
}
See this document for details.
The $uri variable includes a leading /, so your constructed pathnames contain a // which may be why they fail.
For example:
location / {
root /home/tom/git/project/v3/$subdomain;
try_files /public$uri /workspace/public$uri =404;
}

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.

Serving static HTML files in Nginx without extension in url

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;
}

Wordpress blog on Nginx - CSS and JS files not found

I built a custom PHP/MySQL website (www.mywebsite.com) to which I attached a wordpress blog (www.mywebsite.com/blog). I've been trying so many different nginx config things and read so many blog articles that I'm lost now :-)
The main website part is working fine. All my url rewrites work fine.
But on the blog part, I don't know what to do anymore for Wordpress to work properly.
Here's my last conf for the blog part. Permalinks work but the css and js files cannot be found.
location /blog {
root /var/www/mywebsite.com/blog/;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
You can add a location for them, and a expires as well.
location ~* \.(js|css|xml|txt)$ {
expires 14d;
root /to/the/folder/they/live/in;
access_log off;
}
Discovered an awesome blog that I definitely recommend => http://blog.martinfjordvald.com/
After a read through, I updated the root in my location block like this:
location /blog {
root /var/www/mywebsite.com;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
Works fine now !

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