Nginx error 404 when using autoindex - nginx

I'm completely new to nginx. I've installed nginx on windows pc.
What I want to do is server a list of files in D:\ on localhost:8082/list.
If I use the following conf:
server {
listen 8082;
location / {
root D:/;
autoindex on;
}
}
I can correctly see what i want on localhost:8082. But if I change it to:
server {
listen 8082;
location /list {
root D:/;
autoindex on;
}
}
The page localhost:8082/list gives a 404 error.

What you need is alias instead of root.
server {
listen 8082;
location /list {
alias D:/; ##### use alias, not root
autoindex on;
}
}
See Nginx -- static file serving confusion with root & alias

Related

How do i use NGINX to reverse proxy to local react app

I'm trying to do a simple reverse proxy to my local react app with nginx. I can't wrap my head around how this works. Do i need a root variable in location /test or maybe a alias? because nginx is looking in the wrong address. (im running my react app locally at localhost:3001)
Already tried using rewrite /test(.*) /$1 break in the "location /test"-block
this is my nginx.conf:
server {
listen 81 ;
server_name app1.localhost;
location / {
root html;
index index.html index.htm;
}
location /test {
proxy_pass http://localhost:3001;
}
}
heres the console log when i try to enter app1.localhost:81/test:
Just go with two server blocks:
server {
listen 81 default_server;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name app1.localhost;
location / {
proxy_pass http://localhost:3001;
}
}

Sub subdomain overridden by subdomain in NGINX

I have a server with Nginx.
I would like to set up two sites:
backend.mysite.com
staging.backend.mysite.com
Here is my server blocks config:
www.backend.mysite.com:
server {
listen 80;
server_name backend.mysite.com www.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8800/;
}
}
server {
listen 8800;
server_name my.ip.address;
root /projects/backend/production/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
www.staging.backend.mysite.com:
server {
listen 80;
server_name staging.backend.mysite.com www.staging.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8900/;
}
}
server {
listen 8900;
server_name my.ip.address;
root /projects/backend/staging/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
My problem is that backend.mysite.com is overriding staging.backend.mysite.com. How can I say to Nginx to never override if there is a sub subdomain on my adress?
UPDATE:
I've tried to add another domain (my_other_site.com) in my second config to check if it works:
server {
listen 80;
server_name my_other_site.com www.my_other_site.com staging.backend.mysite.com www.staging.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8900/;
}
}
server {
listen 8900;
server_name my.ip.address;
root /projects/backend/staging/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
When I visit my_other_site.com it works well. The second site is reached as expected.
my_other_site.com, www.my_other_site.com, staging.backend.mysite.com and www.staging.backend.mysite.com have the same DNS A configuration, they are pointing on the same IP.
UPDATE 2:
When I disable www.backend.mysite.com server block, it works. The site staging.backend.mysite.com is working as expected. That mean that indeed the first block overrides the second one.
How can I tell the first server block to not take in account staging.backend.mysite.com? Is there a way to exclude a specific domain name?
Try to give for included config files same names as domain names, e.g.:
aa.domain.com --> aa.domain.com.nginx.conf
bb.domain.com --> bb.domain.com.nginx.conf
so nginx will include and catch in natural alphabetic order
I was running into the same experience, but eventually when I ran sudo nginx -T, it spat out:
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed
So, I edited nginx.conf and uncommented the relevant line:
# ...
server_names_hash_bucket_size: 64;
# ...
I picked 64 because that was what was in the file, commented out. Then I restarted and everything worked.
Then I cleared the cache in my browser.

Nginx: 403 on static files (port 80 only)

When on a higher port, say 8000, my static files are loaded fine. When I run the server on port 80 (using sudo), I get 403 errors. Any idea why this would be?
My only thought is that its something to do with running as root, the file permissions are all normal, even going -777- doesn't change the error.
# the upstream component nginx needs to connect to
events {
worker_connections 1024;
}
http{
upstream django {
server 127.0.0.1:8001;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
root ~/my/path/;
# the domain name it will serve for
server_name 127.0.0.1;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias _; # your Django project's media files - amend as required
}
location /static/ {
autoindex on;
root /my/path/static/;
}
location ~ \.css {
root /my/path/static/;
}
location ~ \.js {
root /my/path/static/;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include ../uwsgi_params;
}
}
}
You need to add this context to your content directory:
chcon -R -t httpd_sys_content_t /my/path/static
Or you need to disable Selinux.
Edit /etc/sysconfig/selinux file to disable selinux permanently.
setenforce 0 to disable selinux on-the-fly.

nginx root directive causing 404

I ham trying to make http://example.com serve http://example.com/home.html from /home/ubuntu/mysitedir/home.html.
I have the following conf file which successfully redirects everything to https, and proxies to uwsgi. The http->https redirection works fine, and the uwsgi proxy works, but http(s)://example.com/, http(s)://example.com/home.html, http(s)://example.com/index.html, and http(s)://example.com/index.htm are all 404
Any pointers as to what I can try?
Here is my conf file:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
root /home/ubuntu/mysitedir/;
index home.html;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example_combined.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
root /home/ubuntu/mysitedir/;
index home.html;
location /images/ads {
alias /home/ubuntu/mysitedir/images/ads/;
}
location /images {
alias /home/ubuntu/mysitedir/images/;
}
location /static {
alias /home/ubuntu/mysitedir/static/;
}
location / {
alias /home/ubuntu/mysitedir/;
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
}
Thanks.
location / is the catch-all. You could try the try_files directive like this:
location #wsgisite {
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
location / {
index home.html;
try_files $uri $uri/ #wsgisite;
}
Any thing coming into the base location will first check to see if it is a file, or a directory with an index (home.html) file in it, and if not, then pass it on to the #wsgisite.
This should also make the other three location directives obselete, since nginx will be checking for the files first before passing it to the wsgi block.

Nginx allow ip to access all paths

Is there any way to allow an ip to access all paths and files on the server?
I mean really any path or file.
Because i have some problems with mono that it tells me that my file is forbidden
Yes you can do this, but it's really not recommended, especially if this is a live/production website. You simple have to set autoindex to all for the root path. Setup your list of addresses with geo and an if statement.
geo $allow_addresses {
default 0;
144.0.0.0/24 1;
}
server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/log/access.log;
root /path/to/root;
location / {
index index.php index.html index.htm;
}
location / {
if ($internals) {
autoindex on;
}
}
}

Resources