nginx doesn't cache json, but caches .jpg file - json

My nginx server if configured like this:
......
server {
# Status page
location /nginx_originserver {
stub_status on;
}
listen 80;
location ~ ^/1 {
proxy_pass http://10.10.52.126:1239;
proxy_cache api_cache;
}
......
}
In this case, when I browse http://localhost/1/thumbnail.jpg, the image file is cached. But when I change the proxy to a location which returns json like below and browse http://localhost/1/api_service, the json file is not cached, why just the image file is cached but not json, how to cache the json file?
location ~ ^/1 {
proxy_pass http://10.10.52.126:8090;
proxy_cache api_cache;
}

Did you try proxy_cache_valid 200 1d;:
location ~ ^/1 {
proxy_pass http://10.10.52.126:8090;
proxy_cache api_cache;
proxy_cache_valid 200 1d;
}
Link

Related

how to serve xml file as a static content in nginx

I have a test.xml file stored in the location
/deploy/projects/backend/assets. I tried to serve this file as a static content when someone accesses this using link https://xyz/assets/test.xml. However, it gives error "404 Not Found". My nginx config file is as below:
cat nginx/server.conf
http {
include /etc/nginx/mime.types;
client_max_body_size 100m;
server {
listen 80 default_server;
server_name _;
location ~ ^/assets/ {
root /deploy/projects/backend;
}
}
}

nginx custom maintenance 503 page with css and image

I have a nginx virtual server with a custom 503 used for maintenance.
The html page uses some fonts from google fonts, bootstrap, but also a css and an image from the local server.
When the page is loaded, the linked css is not loaded. Using Firefox web tools I can see for the Error 503.
Trying direclty the css URL, the css is loaded by the browser.
The image is correclty displayed even if has an error 503.
This is my nginx configuration:
if (-f /home/user-site/www/MainWebSite/releases/current/.maintenance) {
return 503;
}
if (!-d /home/user-site/www/MainWebSite/releases/current) {
return 503;
}
# error_page 503 /maintenance.html;
# location = /maintenance.html {
# root /home/user-site/www/MainWebSite/htdocs;
# }
error_page 503 #maintenance;
location #maintenance {
root /home/user-site/www/MainWebSite/htdocs;
#rewrite ^(/css)/(.+?)(\.[^.]*$|$) /stuff/css/$2$3 break;
rewrite ^(/img)/(.+?)(\.[^.]*$|$) /stuff/img/$2$3 break;
rewrite ^(.*)$ /maintenance.html break;
}
I suppose that the css cannot be loaded because of the error 503 returned by the server even if the rewrite roule correctly retunr the css.
How I can prevent error 503 on css and images of landing pages?
tanks.
Update!!
Following this solution the configuration works!
if (-f /home/user-site/www/MainWebSite/releases/current/.maintenance) {
set $err503 1;
}
if (!-d /home/user-site/www/MainWebSite/releases/current) {
set $err503 1;
}
error_page 503 #maintenance;
location #maintenance {
root /home/user-site/www/MainWebSite/htdocs/error503;
try_files $uri /maintenance.html =503;
}
location /static {
root /home/user-site/www/MainWebSite;
}
location /media {
root /home/user-site/www/MainWebSite;
}
location /robots.txt {
root /home/user-site/www/MainWebSite/htdocs/robots.txt;
}
location /sitemap.xml {
root /home/user-site/www/MainWebSite/htdocs/sitemap.xml.txt;
}
location ~* \.(css) {
root /home/user-site/www/MainWebSite/htdocs/error503;
# The file will be returned
}
location ~* \.(png|jpg|jpeg) {
root /home/user-site/www/MainWebSite/htdocs/error503;
# The file will be returned
}
location / {
if ($err503 = 1) {
return 503;
}
uwsgi_pass unix:/tmp/uwsgi_MainWebSite.sock;
include /etc/nginx/uwsgi_params;
}

Configuring nginx for single page website with HTML5 push state URL's

How can I configure nginx to redirect all URL's (not prepended with /api or some static resource eg. JS/images) to index.html? Reason is I am using HTML5 push state URL's with a single page application. Meaning content is changed whether AJAX or JS depending on the URL
My current nginx config looks like:
server {
listen 2000;
server_name localhost;
location / {
root /labs/Projects/Nodebook/public;
index index.html;
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect off;
}
}
location / {
try_files $uri /index.html;
}
This will check if the requested file exists and return it. If the file doesn't exist, it will return index.html.
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
mattes answer is almost a solution, however it won't give 404 for missing files (e.g. favicon.icon) as aschepis pointed out.
Nginx will pick the first location that matches. So we can first match for files (which will give 404 if the file does not exist). And after put a location which defaults to index.html for all urls.
location /.+\..+ { # files (assuming they always have a dot)
# use eg alias to serve some files here
}
location / { # url routed by client, client gives 404 for bad urls
try_files $uri /index.html;
}
You need to add to your nginx config file:
rewrite ^(.+)$ /index.html last;
Then say you're using Backbone.js just make sure you re-route any non-defined route to a 404 page:
routes: {
// Other routes
"*path" : "notFound"
},
notFound: function(path) {
// Load 404 template, probably of a cute animal.
}
Source:
http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/

Multiple authentication together with Nginx proxy

I am trying to authenticate several locations together with proxy_pass in Nginx. The Nginx config is following:
server {
listen 443;
server_name example.com;
location /hg/ {
rewrite ^/hg/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8001;
auth_basic "hg";
auth_basic_user_file hg.htpasswd;
location /hg/repo1/ {
auth_basic "hg-repo1";
auth_basic_user_file repo1.htpasswd;
}
location /hg/repo2/ {
auth_basic "hg-repo2";
auth_basic_user_file repo2.htpasswd;
}
}
}
The authentication works ok, but the proxy gets broken in nested locations (repo1, repo2). It seems that proxy_pass config is not inherited. So, Nginx returns 404 (on /hg/repo1 and /hg/repo2).
Any hints?
You need to repeat proxy_pass for each location block.
Also, there is no function to nesting the location blocks. Usually they are not nested.

Nginx to server node.js content + static content

I have an application with both dynamic and static content. I use nginx as a front end for this app. When dynamic content is requested, the request is forwarded to an unix socket (to a node.js app), this part works well. I have added a "location" directive to serve the static content but this part does not work, I get the 404 error each time despite the fact the folder "/home/test/my_app/static" does exist.
This is the nginx conf I have:
upstream test_sock {
server unix:/tmp/test.sock
fail_timeout=0;
}
server {
listen 15000;
client_max_body_size 4G;
server_name localhost domain.com;
keepalive_timeout 5;
location ~ /static/ {
if (!-f $request_filename) {
return 404;
}
if (-f $request_filename) {
root /home/test/my_app/static;
expires 30d;
}
}
location / {
proxy_pass http://test_sock;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
Any idea ?
hmmm... ok, silly thing, I was missing the root directive before the locations ones...