Serving one static file with nginx and auth_request - nginx

I am trying to server a static file for a location. It's returning 404 and I don't know why.
Here is my config (censored a bit)
root /home/git/website/prod/;
location = /login {
index login.html;
}
location / {
auth_request /auth;
try_files $uri $uri/index.html;
}
location = /auth {
internal;
proxy_pass http://127.0.0.1:5000/api/auth/logged_in;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
location /api {
proxy_pass http://127.0.0.1:5000;
}
When I try to access /login it returns 404. Why?

Assuming your login.html is in root folder (/home/git/website/prod/), you could probably use:
location = /login {
try_files /login.html =404;
}
Or replace =404 with /index.html. That part is basically telling nginx what to try if /login.html is not found on the server.

Related

problem using variable in nginx location directive

good evening. i have a question regarding nginx and it is related to the location directive. i currently have this configuration in nginx
server {
server_name ~^(?<account>.+)\.domain\.com$;
root /var/www/html/folder-frontend/;
index index.html;
error_log /var/log/nginx/$account-access.log;
access_log /var/log/nginx/$account-access.log;
location / {
try_files $uri /index.html;
}
location /$account-backend/ {
proxy_pass http://service-backend/;
proxy_set_header HOST $account-backend.domain.co;
proxy_http_version 1.1;
}
}
this means that I have several domains with the ending tenant.domain.com(app1.domain.com, app2.domain.com). with the expression (?.+) I am getting part of the string in the url that interests me and then in the location directive use it to make a proxypass and redirect the requests. but this is not working, I know because when I put in the location what interests me (in this case would be location /app1-backend/) if redirects to the backend service that I have listening in another nginx.
My doubt is, can I use a variable in the location directive of nginx? I tried it that way specified and it does not work.
No, you can't use a variable as location directive argument, even in a regex matching ones. You can try a workaround like
server {
server_name ~^(?<account>.+)\.domain\.com$;
root /var/www/html/folder-frontend/;
index index.html;
error_log /var/log/nginx/$account-access.log;
access_log /var/log/nginx/$account-access.log;
location / {
try_files $uri /index.html;
}
location ~ ^/(?<prefix>[^.]+)-backend(?<suffix>/.*) {
if ($prefix != $account) {
return 404;
}
proxy_pass http://service-backend$suffix$is_args$args;
proxy_set_header HOST $prefix-backend.domain.co;
proxy_http_version 1.1;
}
}

NGINX not serving files

So I have some an NGINX configuration that looks like this:
limit_req_zone $http_cf_connecting_ip zone=api:10m rate=20r/s;
server {
server_name smokese.sh;
server_name www.smokese.sh;
root /home/SmokeSesh/Smoke/dist;
try_files $uri #web;
location /.well-known { try_files $uri =403; }
location /assets {
try_files $uri #web;
}
location #web { proxy_pass http://localhost:8000; }
# API
location /api/ {
limit_req zone=api burst=20 nodelay;
limit_req_status 429;
add_header cache-control "no-store, max-age=0"; # Disable cache on API routes
proxy_pass http://localhost:8080/;
}
}
Now my question is, is what could possibly be stopping it from serving files to /assets. It always just fall's back, and yes root does exist.
I'm not sure if this is an issue with NGINX itself or with my configuration, but help would be greatly appreciated.

Nginx - How to serve a static file to a particular user agent?

I am trying to restrict access to a particular json file for a certain user-agent using the configuration files in Nginx.
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
try_files $uri$args $uri$args/ /index.html;
}
location /v1/ {
proxy_pass http://127.0.0.1:8118;
proxy_pass_header Server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location = /v1/apischema {
root /usr/share/nginx/json;
if ($http_user_agent ~ useragentname) {
try_files /openapi.json;
break;
}
}
}
The idea is to restrict access to openapi.json for an internal user.
Please let me know where I am going wrong.
try_files can be used in a server or location block. See Context: in this document.
There are a number of ways to achieve this.
Using if:
location = /v1/apischema {
if ($http_user_agent ~ useragentname) {
rewrite ^ /openapi.json last;
}
}
location = /openapi.json {
internal;
root /usr/share/nginx/json;
}
The internal directive prevents this URI being accessed directly. See this caution on the use of if.
Or using map with try_files:
map $http_user_agent $apischema {
~useragentname /openapi.json;
default /nonexistant;
}
server {
...
location = /v1/apischema {
root /usr/share/nginx/json;
try_files $apischema =404;
}
}
See this document for details.

Serve both static website & SPA (node.js) with NGINX

I need a way to serve both static html website and SPA using NGINX.
https://website.com -> should be the static index.html page.
https://website.com/register -> should point to SPA powered by node.js webserver.
So, if there's an appropriate static html route it should be served with NGINX, any other route should be served by node.js webserver.
e.g.
https://website.com/about -> served by NGINX because there's about.html
https://website.com/register -> served by node.js because there's no appropriate static page for this route (there's no register.html).
I've created the following configuration, but it doesn't seems to work properly.
upstream backend {
server localhost:8080;
}
server {
listen 80;
server_name website.com www.website.com;
root /var/www/website.com;
location = / {
try_files /index.html =404;
}
location = /index {
return 404;
}
location / {
try_files $uri/index.html =404 #backend;
}
location #backend {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Your configuration could probably be simplified to this:
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ #backend;
}
location #backend {
...
}
You could try something like
location / {
try_files $uri $uri.html $uri/ #backend;
}
Breaking it down for you. The try_files directive
Checks the existence of files in the specified order and uses the first found file for request processing;
So
$uri - look for the exact file as defined in the path. Useful for static files like css, images, js, etc..
$uri.html look for the exact file with a .html extension. This would get your about.html
$uri/ - looks for it to be a directory then uses the index directive to look for index or index.html
#backend - send to the proxied application would be responsible for returning 404

nginx: location = / {} error

My nginx vhost config content:
server {
listen 80;
server_name t.xianzhi.xxx.domain;
access_log /data/log/nginx/t.xianzhi.xxx.domain_access.log main;
location ~ /\. {deny all;}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location = / {
root /data/web/static/html;
index index.html;
}
location / {
proxy_pass http://127.0.0.1:9000;
}
location = /favicon.ico {
access_log off;
root /data/web/static/;
}
location = /apple-app-site-association {
add_header Content-Type "text/html; charset=utf-8";
root /data/web/show/public/wap/;
}
location ~ \.(css|js|png|jpg|woff|ttf)$ {
root /data/web/static;
expires 10d;
}
}
As the config, I want to server the path / to /data/web/static/html/index.html and server the others to proxy_pass.
The truth is the path / is 404 not found and the others is successful.
The log is :
24/Aug/2017:10:49:43 +0800 10.5.17.37 t.xianzhi.xxx.domain - curl/7.51.0 - request:GET / HTTP/1.1 bbs:233status:404 upad:127.0.0.1:9000 rspt:0.017 rqtt:0.017 request_body:-
So, the / is passed to proxy.
Some info:
The nginx version: nginx/1.10.1
So, how to fix it?
The problem is your = / location block. If you consider the section
location = / {
root /data/web/static/html;
index index.html;
}
You specify the root and index.html, but you don't server anything. So you should change it to
location = / {
root /data/web/static/html;
index index.html;
try_files /index.html =404;
}
or
location = / {
root /data/web/static/html;
try_files /index.html =404;
}

Resources