How can I add header every location if query exist Nginx - nginx

I have two url
http://localhost/?shop=test
http://localhost/login?shop=test
first url is working. But second url coming 404 nginx page. how can I fix this problem. I want to every location come header if exist shop query
server {
listen 8081 default_server;
listen [::]:8081 default_server;
server_name _;
location / {
if ( $arg_shop ) {
add_header Content-Security-Policy "frame-ancestors https://$arg_shop";
}
root /home;
index index.html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html?$query_string;
}
}

The problem with using if inside a location, is that it doesn't work the way you expect.
You can use a map to define the value of the add_header directive. If the argument is missing or empty, the header will not be added.
For example:
map $arg_shop $csp {
"" "";
default "frame-ancestors https://$arg_shop";
}
server {
...
add_header Content-Security-Policy $csp;
location / {
...
}
}

I fixed like that
server {
listen 8081 default_server;
listen [::]:8081 default_server;
server_name _;
location / {
error_page 404 = #error_page;
if ( $arg_shop ) {
add_header "Content-Security-Policy" "frame-ancestors https://$arg_shop";
}
root /home;
index index.html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html?$query_string;
}
location #error_page {
add_header "Content-Security-Policy" "frame-ancestors https://$arg_shop";
root /home;
index index.html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html?$query_string;
}
}

Related

How to Use a Single Custom Error Page for all 404 Requests

I have created a custom 404 page for resources that dont exist.
It works with all endpoints except the ones which have /api/v1/, where I get the default NGINX 404 page.
I have domain.name.conf file in /etc/nginx/conf.d/:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
location /api/v1/ {
proxy_pass http://localhost:8080/;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}
On adding the $try_files directive inside /api/v1/, "Hello, World!" from the backend REST API is not displayed at /api/v1/hello, even though its a valid endpoint. Instead I get the custom 404 error page:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
location /api/v1/ {
proxy_pass http://localhost:8080/;
try_files $uri =404;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}
How can I use a single custom error page for all non existing resources ?
Thanks to Richard Smith's comment, the conf file look like:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
proxy_intercept_errors on;
location /api/v1/ {
proxy_pass http://localhost:8080/;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}

nginx redirection to index page

I am struggling to implement an automatic nginx redirect from non index pages to my index page, with the exception of /admin
For instance, example.com/test should redirect to example.com, but example.com/admin should not redirect to example.com
This is my current nginx configuration file:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
I have tried adding a try_files statetement within my location / block, and other things, but none seem to work. Am I missing something?
You are trying to mix proxy_pass with try_files, it won't work within the same location block. You can use named location instead and rewrite any URI that doesn't start with /admin to a root one using negative regex assertion:
location / {
try_files $uri #app;
}
location #app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
You don't need the separate location /media { ... } or location /static { ... } blocks, because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
Instead you just need to define the common server root (outside of any location blocks):
root /var/www/example.com;
You are also don't need to use the proxy_ssl_server_name directive since you are not proxying your request to the upstream with HTTPS protocol.

Nginx configuration causing too many redirects

I have a conf file with example.com as the root. In the example.com directory, there is an html, css, img, and js folder. I understand this deviates from the traditional html directory as root. I have tried many different configurations (using regex based on filetypes, variables, etc.) but I always get too many redirect errors. Can anyone help on a good conf file for this type of directory structure? Here is my conf file currently.
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = error.html?error=404;
location ~ /.well-known {
allow all;
}
location / {
location ~* \.(html|php)$ {
root html/;
}
location ~* \.css$ {
root css/;
}
location ~* \.js$ {
root js/;
}
location ~* \.(png|jpeg|gif)$ {
root img/;
}
try_files $uri =404;
}
}
Thanks in advance for any help!
Here is the configuration I ended up using:
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = /html/error.html?error=404;
location ~ /.well-known {
allow all;
}
location = / {
try_files /html/index.html =404;
}
location / {
location ~* \.(html|php)$ {
try_files $uri /html/$uri =404;
}
location ~* \.css$ {
try_files $uri /css/$uri =404;
}
location ~* \.js$ {
try_files $uri /js/$uri =404;
}
location ~* \.(png|jpeg|gif)$ {
try_files $uri /img/$uri =404;
}
try_files $uri =404;
}
}
My problem was that all of my redirects were using relative pathing (such as try_files html/$uri) instead of absolute pathing from the site root (/html/$uri). This lead to redirects like /html/html/html/...
I thought that if I used an absolute path, it would be absolute to the root of the server, and not the site.
My only issue now is that my parameter on my error page redirect (?error=404) doesn't work with absolute pathing, but that not a huge deal.

Nginx 404 issue when serving static files of a sub-directory

I have 404 error when trying to access CSS and JS files of the sub static directory. I tried to use =, or ~ but it still does not work.
mydomain/sub/index.html is served.
mydomain/sub/css/style.css is not served.
Here is the Nginx configuration file :
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
server {
# Global
listen 443 ssl http2;
server_name www.example.com example.com;
charset utf-8;
# Logs
...
# SSL
...
# OCSP Stapling
...
# HSTS
....
# Robots
location /robots.txt {
return 200 "User-agent: *\nAllow: /";
}
# Sub
location /sub {
alias /www/sub;
index index.html;
try_files $uri $uri/ =404;
}
# Page Speed
include /etc/nginx/pagespeed.conf;
# Set expiration policy
location ~* \.(?:ico|css|js|gif|jpe?g|png|woff)$ {
expires 7d;
add_header Pragma public;
add_header Cache-Control "public";
proxy_pass http://example;
}
# Location
location / {
if ($request_method = POST) {
return 500;
}
proxy_pass http://mydomain;
}
}
in my case, sub directory should use sub location, wish help you.
location /company/default/static/ {
alias /data/pro/static_deployment/ ;
autoindex off;
location ~* \.(js|css|png|jpg|jpeg)$ {
expires 7d;
}
}

How can I force autoindex and ignore index.html files

When nginx serves a directory with autoindex, it will list files, but when index.html exists, the browser will load that file. I want it to ignore it.
server {
listen 80;
server_name herbert;
location / {
root /srv/www;
index index.htm index.html;
add_header Cache-Control no-cache;
expires 300s;
}
location /site-dumps/ {
root /srv/www/;
autoindex on;
}
}
It's not possible. You should move other files in other directory and create an iframe in index.html.
Something like that:
index.html
<iframe src="/site-dumps_files"></iframe>
nginx.cnf
server {
listen 80;
server_name herbert;
location / {
root /srv/www;
index index.htm index.html;
add_header Cache-Control no-cache;
expires 300s;
}
location /site-dumps/ {
root /srv/www/;
}
location /site-dumps_files/ {
root /srv/www/;
autoindex on;
}
}
I hope it useful for you.
this worked for me:
server {
listen 80;
listen [::]:80;
server_name localhost;
autoindex on;
index =404;
location / {
root /path_to_www_dir;
}
}
Put: index main.html main.htm; instead of index index.htm index.html;

Resources