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;
Related
I'm working on a docker Nginx server on local, this is my conf.d/default.conf configuration:
server {
listen 80;
listen [::]:80;
server_name localhost;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/ssl/certs/localhost.pem;
ssl_certificate_key /etc/ssl/private/localhost.key;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
}
This configuration works perfectly, but for last "location" block, which works as an "userDir" directive, I have to type an URL like this: https://localhost/~user1/
In order to remove that trailing slash, I tried:
location ~ ^/(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
This solution works fine and answers with the index.html content in "/home/user1/public_html" by using the URL https:/localhost/user1 which is how I want it to work, but then, root content (https:/localhost/) becomes 404 Not Found.
Hope I did explain it well and someone has a solution! Thank you in advance!!
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;
}
}
here is my question
I am learning nginx recently
And I don't know why I can handle my config about location
here is my nginx.conf
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root /Users/billyliou/web-site;
location / {
root html;
index index.html;
}
location /demo {
index index.html index.htm;
try_files $uri $uri/ /demo/index.html?s=$uri&$args;
}
location /test {
index index.html index.htm;
try_files $uri $uri/ /test/index.html?s=$uri&$args;
}
...
I put two folders into '/Users/billyliou/web-site'
one is demo , the other is test.
Both contain index.html.
And when I type localhost:8080, it shows nginx default page.
When I type localhost:8080/demo it shows demo page.
but when I type localhost:8080/test , it shows demo page too.
the last one is beyond my expectation.
How can I set this config?
Try this
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /demo {
root /Users/billyliou/web-site/demo;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location /test {
root /Users/billyliou/web-site/test;
index index.html index.htm;
try_files $uri $uri/ /index.html?s=$uri&$args;
}
location / {
root /Users/billyliou/web-site;
index index.html;
}
...
}
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.
Is it possible to optimize/minimize the config posted below?
I feel that it should be possible to merge all the redirects into something more simple.
http:// & http://www & https://www > https://
Though I've had issues and settled.
I understand variables are not supported in NGINX config, so I have to manually define the log locations for example. Would there be a way to set a default location for all vhosts?
I use the same ssl-params.conf file for all vhosts. Can this be defaulted and disabled on a per-vhost basis?
# Redirect http:// & http://www to https://
server {
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
# Redirect https://www to https://
server {
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com/$request_uri;
}
# Main config
server {
listen 443 ssl;
server_name example.com;
# SSL config
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
# Error logs
access_log /srv/logs/nginx.access.example.com.log;
error_log srv/logs/nginx.error.example.com.log;
# Root dir
location / {
root /srv/example.com/_site/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
# Caching
location ~ .php$ {
root /srv/example.com/_site/;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
root /srv/example.com/_site/;
expires 365d;
}
location ~* \.(pdf)$ {
root /srv/example.com/_site/;
expires 30d;
}
# SSL
location /.well-known {
allow all;
}
}
I understand variables are not supported in NGINX config, so I have to manually define the log locations for example. Would there be a way to set a default location for all vhosts?
Yes, just define it in the http context of your config or stick with the default of your distro (e.g. /var/log/nginx/access.log).
I use the same ssl-params.conf file for all vhosts. Can this be defaulted and disabled on a per-vhost basis?
It works the other way around you enable it where you need it through the include directive.
Here is a shorter config (untested):
http {
error_log /srv/logs/nginx.error.example.com.log;
access_log /srv/logs/nginx.access.example.com.log;
index index.php index.html index.htm;
server {
listen 80;
listen 443 ssl;
server_name .example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
root /srv/example.com/_site/;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
location / {
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
}
location ~* \.(jpe?g|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
try_files $uri $uri/ /index.php?$args;
}
location /.well-known {
allow all;
}
}
}