i am using nginx for static files and want that all files should be cached by browser, but not index.html. Tried following config but i get cache response header for index.html also.
how can i change config?
server{
location = / {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
add_header 'Cache-Control' "public, max-age=3600";
}
}
To understand the logic of try_files $uri $uri/ ... directive (and the whole nginx behavior) I recommend you to read this answer at ServerFault. The essential thing is
The very important yet absolutely non-obvious thing is that an index directive being used with try_files $uri $uri/ =404 can cause an internal redirect.
This is what happens with your current config. The $uri/ argument of the try_files directive causes nginx to make an internal redirect from / to /index.html, and that URI in turn is processed by the location / { ... }, not the location = / { ... }! To achieve what you want you can use
location = /index.html {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
add_header 'Cache-Control' "public, max-age=3600";
}
Moreover, since the try_files $uri $uri/ =404; is default nginx behavior, you can further simplify it to
location = /index.html {}
location / {
add_header 'Cache-Control' "public, max-age=3600";
}
Related
When we try direct urls like localhost/page1 nginx throwing me 404 page not found.
we are using react-router, webpack and nginx.
in nginx config we tried
location /{
try_files $uri index.html;
}
location /{
try_files $uri $uri/ =404;
}
location /{
try_files $uri $uri/ index.html;
}
here is my webpack configuration
devServer: {
historyApiFallback: true,
host : '0.0.0.0'
},
but nothing has worked,
Am I missing anything here?
Try this
location / {
try_files $uri /index.html;
}
I want to match the exact location in nginx, if the location has extra text or parameters it should show a 404 page. for example http://localhost:8080/yes shouldn't display the index page but show 404 page. Here's my .conf file
root /var/www/assets/;
index index.html;
location / {
default_type text/html;
try_files $uri $uri/ /index.html =404;
}
location /data {
default_type text/html;
try_files $uri $uri/ /data.html =404;
}
location /voice {
default_type text/html;
try_files $uri $uri/ /voice.html =404;
}
for example I want to match /data only, no /dataaa or /datas and show 404 page if the location doesn't match the above locations.
If you want an exact match to /data then your location need to be specified with a = like so
location = /data {
default_type text/html;
try_files $uri $uri/ /data.html =404;
}
From the Nginx manual about location
[D]efining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.
(emphasis mine)
I have the following code:
server {
.....
root /user/share/nginx/html;
rewrite ^(/.*)\.html(\?.*)?$ $1$2 redirect;
location / {
try_files $uri/index.html $uri.html $uri index.html =404;
}
In the rewrite I remove the .html extension from file
In location I uri corresponding file is not found I offer a set of other options.
The last one before the error code is index.html; index.html exist but always I get the 404;
All Nginx URIs begin with a leading /. Use: /index.html.
For example:
location / {
try_files $uri/index.html $uri.html $uri /index.html;
}
I have a 3 type of paths /api, /,/some_path_here
If user have requested page with empty path (/) I want to redirect to login .
So I have a config file look like this
location /api {
try_files $uri $uri/ /index.php?$args;
}
location /[0-9a-z] {
try_files $uri $uri/ /index.html;
root /var/www/cabinet/client/dist;
}
location / {
return 301 https://my_domain.com/login;
}
But when trying to request https://my_domain.com/ its redirect to https://my_domain.com/login with error ERR_TOO_MANY_REDIRECTS
How can I solve this error?
Here is a solution
location /api {
try_files $uri $uri/ /index.php?$args;
}
location / {
try_files $uri $uri/ /index.html;
root /var/www/cabinet/client/dist;
}
location = / {
return 301 $scheme://$server_name/login/;
}
So I've got 2 routes, and the first one doesn't stop the route matching, as the docs say it should:
location ^~ /p/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ /v/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ / {
root /www/domain.com/php_www/;
try_files $uri $uri/ /index.php;
location ~* \.(?:php|html)$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
auth_basic "Staging";
auth_basic_user_file /www/.htpasswd;
}
So if I have a url like this:
http://domain.com/p/1234567890
It matches the last route and not the first route. The problem surfaced because one of our guys added a page to the application:
http://domain.com/privacy
This was picked up by the FIRST route?? Which is where the problem is coming from.
The problem I'm having is with ^~. In the docs, it says that once this matches, it will stop matching, however the last route is always the one that loads.
Any ideas?
Upgraded to latest nginx, and re-ordered some of the directives and everything is working now.