nginx - Serve file conditionally without redirection - nginx

I'm currently building a multi-domain cms in rails. Since this content is the same until the next change I'd like to do caching via static files.
The public directory with some cached pages of foo.com and baz.com (/ and /asdf in both cases):
public/
assets/
cms.css
sites/
foo.com/
assets/
screen-some-hash.min.css
index.html
asdf/
index.html
baz.com/
assets/
screen-some-hash.min.css
index.html
asdf/
index.html
What I want to do is the following:
redirect www to non-www (works)
If the requests contains a subdomain (cms, admin, whatever):
If the path contains /assets serve the file in public/assets and set the expire stuff to 30d or so. No problem here since /assets = public/assets and public/ is the passenger root.
Everything else: handle it via rails, no special caching or anything required.
For all other requests (meaning no subdomain):
If the path contains /assets serve the file in public/sites/$host$request_uri and set the expire stuff to 30d or so. Everything else: check for public/sites/$host$request_uri or fall back to the rails app.
I have never worked with nginx conditionals other than the www/non-www redirects and don't really know what I have to do for the conditions mentioned above. If at all possible, I don't want to use redirects for the cached stuff (ie redirection to /sites/foo.com/asdf), instead I'd like to have nginx serve this file directly when going to http://foo.com/asdf.
Further: I don't want to hardcode the hostnames as I'd like to handle an unknown amount of domains. I also don't want to use more than a single rails application for this.

Got something that works, not 100% but good enough for now.
server {
listen 80;
server_name *IP*;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
location ~ ^/(assets)/ {
try_files /sites/$host$uri $uri #passenger;
root /home/cms/app/current/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
try_files /sites/$host$uri/index.html /sites/$host$uri $uri #passenger;
root /home/cms/app/current/public;
}
location #passenger {
access_log /home/cms/app/shared/log/access.log;
error_log /home/cms/app/shared/log/error.log;
root /home/cms/app/current/public;
passenger_enabled on;
}
}

For subdomains, this should do the trick:
server {
server_name ~^(?<subdomain>.+)\.example\.com$;
access_log /var/log/nginx/$subdomain/access.log;
location /assets {
expires max;
}
location / {
proxy_pass http://your_rails_app;
}
}
Not really sure about the proxy_pass setting as my only experience with Ruby apps is Gitlab, which I'm running this way. I hope this helps at least a little.
server {
server_name example.com;
location /assets {
root /public/sites/$hostname/$request_uri;
expires max;
}
}
You'll have to add your own settings and play with it a little as I don't have a chance to actually test it now. But it should show you the way.

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 to route different webservers to different URL using nginx

creating a website for my self and need to host projects.
Basically, i hhave different projects with different framework. ie, Flask, Django, Node.JS and some html file projects. I would like to host them at projects.domain.com/<project name>
I tried to set server_name projects.domain.com/asdf but in error.log it says, server name "projects.domain.com/asdf" has suspicious symbols
Next up, i tried to nest location blocks (which i presume isn't how its supposed to be)
location /asdf {
location /static/ {
root blah blah;
}
location / {
..
proxy_pass http://localhost:3000 ;
}
}
But, this errors out saying location static is outside asdf
Some suggested to alias instead of root in the location /static/ block, but that doesnt work too.
Any help is appreciated :)
First of all a server_name can not contain URI segments. So a hostname or IP should be used as a value.
If you want to mix different local directories and proxy-locations a configuration could look like this.
Notice: Your location URI (/one, /two) will be appended to the root path.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not be part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
server {
server_name project.domain.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /one/ {
alias/var/www/html/project1/;
index index.html;
}
location /two/ {
alias/var/www/html/project2/;
index index.html;
}
}

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

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

Nginx location block for specific path and certain file types

I am having trouble defining a location block for certain paths and file types.
I am using wordpress and using a plugin which generates dynamic sitemaps..It redirects to path like sitemapindex.xml, which do not actually exist and nginx is trying to serve it statically.
I need to be able to pass this to apache
I need to send anything that is http://example.com/blog/*.xml to apache. This is what i am trying, which does not work.. so for instance:
http://example.com/blog/post.xml or http://example.com/blog/sitemapindex.xml
nginx config
server {
location ~* ^/blog/*.xml$ {
include /etc/nginx/proxy_params;
proxy_pass http://127.0.0.1:8080;
}
}
what is the correct syntax
Thanks
I had similar problem with my images. In my applications, images were being served from two different locations.
You can specify different sources based on url pattern. Your solution would then look something like this.
location ~* ^/blog/.+\.(xml)$ {
root /some/path/;
expires 90d;
}
location ~* \.(xml|js|jpg|png|css|html|otf|eot|svg|ttf)$ {
root /some/other/path/;
expires 30d;
index index.html;
}
Gotta escape that period
server {
location ~* ^/blog/.*\.xml$ {
proxy_pass http://127.0.0.1:8080;
}
}

Resources