directory index of "/opt/eds/web/html" is forbidden - nginx

To configure nginx, as follows
location ~ "^/[A-Z0-9]{32}" {
alias /opt/eds/web/html;
index index.html index.htm;
}
nginx Abnormal log
`2019/12/17 23:22:56 [error] 28874#28874: *4 directory index of "/opt/eds/web/html" is forbidden`
but modify nginx configuration, as follows
location /25DE5ADF310211E9BDB874D435BEC0BA {
alias /opt/eds/web/html;
index index.html index.htm;
}
No problem with access

When alias is used with a regular expression location, you need to capture the remainder of URI and append it on the alias value.
For example:
location ~ "^/[A-Z0-9]{32}(/.*)?$" {
alias /opt/eds/web/html$1;
index index.html index.htm;
}
See this document for details.

Related

nginx regex location

we need to serve the static content through nginx. below is our location directive:
location ~ /app$ {
root /srv/deployments/app/build/;
index index.html;
}
now we are expecting any thing like /app/login,/app/signup will be served by this location directive. but when we try to access /app/login we 404 error. and in addition to above location directive if we define below location directive, then it work.
location ~ /app/login$ {
root /srv/deployments/app/build/;
index index.html;
}
as there can be multiple routes, how can we define a location directive with regex so that any which match /app/.* should served by single location.
Please check if this link helps.
It suggests (specific to that question)
location ~ ^/sitename/[0-9a-z]+/index.php$ {
fastcgi_pass phpcgi;
}
where:
^ -> Start of string
[0-9a-z]+ -> matches all the characters in this range
index.php$ -> end of string with index.php

Nginx multiple domains shared resources

I have the following folders:
/web/domain1/
/web/domain2/
/web/shared/
I want domain1 and domain2 to share static files from /web/shared/ but I am having trouble creating the mapping in nginx.
domain1: /assets/ mapped to /web/shared/
domain2: /admin/assets/ mapped to /web/shared/
server{
server_name domain1;
root /web/domain1/;
location / {
rewrite /assets/(.*) /web/shared/$1;
}
}
This gives me 404 error.
Define a location for URIs that begin with /assets/ (see this document for details). Use the alias directive, as the root directive cannot be used in this case (see this document for details).
For example:
location /assets/ {
alias /web/shared/;
}
This works
location /assets/(.*) {
alias /web/shared/$1;
}

How can I manually set static html files for each location block

I am trying to play with nginx. I am trying to serve a particualar index.html files for each location declared on my configuration file like
location / {
root /usr/src/seo/homepage;
}
location ~ /mypage {
root /usr/src/seo/mypage;
}
location ~ /mypage2 {
root /usr/src/seo/mypage2;
}
Where each of the folder location specified has it's own index.html file. But when I try to access mypage and mypage2, nginx returns 404. I am expecting it to render it's respective index.html
UPDATE!!!
Solved it using alias like:
location / {
alias /usr/src/seo/homepage;
}
location ~ /mypage {
alias /usr/src/seo/mypage;
}
location ~ /mypage2 {
alias /usr/src/seo/mypage2;
}
From the docs:
To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive
I.e. when /mypage is requested, nginx tries to find /usr/src/seo/mypage/mypage.
To address that, location blocks for mypage and mypage2 should look something like
location ~ /(mypage|mypage2) {
root /usr/src/seo;
}
That however requires the request to end with a slash / for index directive to work. So it might be a good idea to include try_files:
location ~ /(mypage|mypage2) {
root /usr/src/seo;
try_files $uri $uri/ =404;
}

nginx serve multi index files from location

I want to archive that serving variable html file through different uri, below is my config.
server {
listen 8888;
server_name localhost;
location / {
root html/test
index foo.html
}
location /another {
root html/test
index bar.html
}
}
I want request for localhost:8888/another then response bar.html which present in my test directory, but I'm failed :(
how could I fix above config, thanks for your time.
The filename is constructed from the value of the root directive and the URI. So in this case:
location /another {
root html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/another/bar.html.
If you want the value of the location directive to be deleted from the URI first, use the alias directive.
location /another {
alias html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/bar.html.
See this document for details.

nginx redirect loop, index.html

This seems ridiculous but I've not found a working answer in over an hour of searching.
When I access "http://oa.wechat.com/screen/index.html", it will cause a 301 redirect loop, like this:
"GET /screen/ HTTP/1.1" 301
"GET /screen/index.html/ HTTP/1.1" 301
"GET /screen/index.html/index.html/ HTTP/1.1" 301
...
nginx verson: 1.5.6
nginx.conf
server {
listen 80;
server_name oa.wechat.com;
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
}
Could anyone tell me the reason? Thanks very much.
i have checked nginx document. the right usage of 'alias':
# use normal match like this
location /i/ {
alias /spool/w3/images/;
}
# use regex match like this
location ~ ^/download/(.*)$ {
alias /home/website/files/$1;
}
the wrong way to use 'alias' is:
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
In this case, the request would be considered as a directory request, not file request, which will lead a redirect loop.
Anyway, Thanks Flesh very much!
It's already trying to access index.html in that directory because it's the default of nginx's index directive. The problem is that you're using the index directive within a location block where it has a special meaning and executes an internal redirect (as documented).
Unless you know what you're doing, set the index directive within the server block. We end up with the following server block (be sure to read the comments).
server {
# Both default values and not needed at all!
#index index.html;
#listen 80;
server_name oa.wechat.com;
# Do not use regular expressions to match the beginning of a
# requested URI without protecting it by a regular location!
location ^~ /screen/ {
alias /data/screen/static/;
}
}
location examples
server {
# Won't work because the /data is considered the new document root and
# the new location matches the regular expression again.
location ~ ^/screen/ {
alias /data/screen/static/;
}
# Should work because the outer location limits the inner location
# to start with the real document root (untested)
location / {
location ~ ^/screen/ {
alias /data/screen/static/;
}
}
# Should work as well above reason (untested)
location / {
location ~ ^(/screen/) {
alias /data$1static/;
}
}
# Might work as well because we are using the matching group
# VERY BAD because we have a regular expression outside any regular location!
location ~ ^(/screen/) {
alias /data$1static/;
}
# Always works and allows nesting of more directives and is totally save
location ^~ /screen/ {
alias /data/screen/static/;
}
}
Weblinks
alias documentation
index documentation
location documentation
you should move ^ location modifier from ^/screen/, then add ^ before ~, like this:
`location ^~ /screen/ {
alias /data/screen/static/;
index index.html;
}`

Resources