Serving static HTML files in Nginx without extension in url - nginx

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

Related

Nginx alias still points to and loads from root directory

I have a django backend and react frontend.
I want to serve the react on / and use /admin, /api and /auth for Django. Here's what I have in my Nginx.
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/frontend;
index index.html index.htm;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
With the above, the expected behavior is
/ uses the default root folder, /home/user/folder/frontend and loads the built index files from react accordingly
/(admin|api|auth) points to django
/static loads static files saved in the /home/user/folder/backend/staticfiles folder.
So not sure why when I hit example.com/static/myfile.css, Nginx is going to /home/user/folder/frontend/static/myfile.css
I'd expect none of the above configuration says that's what it should do, so what magic is going on?
I thought this answer was self explanatory enough, yet Nginx keeps doing whatever it likes.
I'm using nginx/1.18.0 (if that matters)
Try adding root inside the location / directive too.
Like this:
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/backend/staticfiles;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
root /home/user/folder/frontend;
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
Also have a look at those QAs:
serve react frontend and php backend on same domain with nginx
Nginx -- static file serving confusion with root & alias
Deploy both django and react on cloud using nginx
from ngix documentation here, it seems you are missing a / at the end of your paths. this trailing / can cause a lot of pain in many languages to be the root cause of many errors.
please give it a try like this:
# for serving static
location /static/ {
alias /home/user/folder/backend/staticfiles/;
}

How can nginx serve static files on a subdirectory and pass the rest to uwsgi?

I've got a directory example.com/data/ that is only served static content at root (i.e. example.com/data/ serves a static webpage, but example.com/data/file is served by a script). I'm using uwsgi to serve the static portion, and have managed to get my script to serve content on its own, but I can't figure out what rules I need to get this behavior because I'm getting an "500 Internal Server Error."
My current (nonworking) configuration looks like:
location = /data/ {
alias /srv/data/index.html;
}
location /data {
gzip off;
include uwsgi_params;
uwsgi_modifier1 9;
uwsgi_pass unix:/run/uwsgi/data.sock;
}
I've tried a few configurations with try_files, but I can't seem to make it work. Do I need to reference one of the directories here differently or use try_files and some named routes somehow?
EDIT: Thanks to #Richard Smith, this configuration works:
location = /data/ {
root /srv/data/;
try_files /index.html =404;
}
location = /data {
gzip off;
include uwsgi_params;
uwsgi_modifier1 9;
uwsgi_pass unix:/run/uwsgi/data.sock;
}

nginx serving static files from root and uploaded files from alias

I run nginx as a reverse proxy server in front of apache.
I need to access uploaded files from frontend in backend so the way to go is to use an alias in nginx site config but static files in backend should be handled directly by nginx. I'm new to nginx so here is my partial config that handles static files. I also specified an alias (/images) but it will not work because it is overwritten by second condition.
How can the two conditions be combined so that nginx handles static files from root (backend app) and uploaded files from frontend app.
In apache config I included an alias for this problem and it works but without nginx in front.
Here is my partial nginx config:
server {
listen 80;
root /var/www/website/backend/www;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm;
server_name admin.website.com www.admin.website.com;
location / {
proxy_pass http://localhost:8080;
include /etc/nginx/proxy_params;
}
#The alias to handle uploaded files(.jpeg, .pdf) from frontend
location /images {
alias /var/www/website/frontend/www/images;
}
#let nginx handle static files from root
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
expires 30d;
}
.
.
.
}
The regular expression location block takes precedence over a prefix location block (unless the ^~ modifier is used). See this document for details.
Try:
location ^~ /images {
root /var/www/website/frontend/www;
}
Note that the root directive is preferred in this case (see this document for details)

Nginx - serving assets from user directories

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.

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