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.
Related
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;
}
}
I have a react frontend and a flask backend.
Currently I serve backend the following way
server {
location / {
try_files $uri #yourapplication;
}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
}
I'd like to configure nginx in a way that would allow me to serve my react app from / and access API from all other routes (i.e. /users is an api endpoint).
Is it a "sensible" set up?
What should my config file look like?
Ended up using the following setup
server {
root /var/www;
index index.html index.htm;
location =/ {
try_files $uri $uri/ /index.html;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# Javascript and CSS files
location ~* \.(?:css|js)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
location /user {
try_files $uri #yourapplication;
}
location /register {
try_files $uri #yourapplication;
}
location /login {
try_files $uri #yourapplication;
}
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
}
In other words, for production it serves static files on =/ and for every other end ponint passes it to flask.
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.
I have uwsgi cache configured, but I want to make it work differently for different locations. My config:
uwsgi_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=mycache:60m inactive=10m;
server {
listen *:80;
server_name thewebsite.loc;
location #uwsgi {
include uwsgi_params;
uwsgi_cache mycache;
uwsgi_cache_valid any 1h;
uwsgi_cache_key $request_uri;
uwsgi_pass unix:///var/run/app/uwsgi.sock;
uwsgi_read_timeout 120s;
}
location / {
try_files $uri #uwsgi;
}
}
let's say, I want to disable cache for a particular location. I add after block for location / another location:
location /dynamic{
uwsgi_cache off;
try_files $uri #uwsgi;
}
But it doesn't work and the view still cached. Is it possible or not supposed to work like this at all?
UPD: I've also tried to configure cache in location /. In this case, it simply doesn't work.
When you access /dynamic the nginx sets uwsgi_cache off but then you redirect to #uwsgi location where you have cache enabled. I think that causes your problem.
Try moving cache config to server context:
uwsgi_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=mycache:60m inactive=10m;
server {
listen *:80;
server_name thewebsite.loc;
uwsgi_cache mycache;
uwsgi_cache_valid any 1h;
uwsgi_cache_key $request_uri;
location #uwsgi {
include uwsgi_params;
uwsgi_pass unix:///var/run/app/uwsgi.sock;
uwsgi_read_timeout 120s;
}
location / {
try_files $uri #uwsgi;
}
location /dynamic {
uwsgi_cache off;
try_files $uri #uwsgi;
}
}
CAUTION: I did not test this config, I'm not sure if it will work
I am configuring a nginx revser proxy. The result should be when user type http://10.21.169.13/mini, then the request should be proxy_pass to 192.168.1.56:5000. Here is the nginx config:
server {
listen 80;
server_name 10.21.169.13;
location = /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
The above location block never worked with http://10.21.169.13/mini. The only location block worked is:
server {
listen 80;
server_name 10.21.169.13;
location / {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
But the above config also match http://10.21.169.13 request which is too board.
What location block will only match 'http://10.21.169.13/mini` and no more?
UPDATE: tried and failed with the following:
location /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
location /mini/ {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
The error is request not found.
Try this out:
server {
listen 80;
server_name 10.21.169.13;
# root /usr/share/nginx/html;
# index index.html index.htm;
location / {
# add something here to handle the root
# try_files $uri $uri/ /index.html;
}
location /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
Let me know if that works for you.