I have several sets of static .html files on my server, and I would like use nginx to serve them directly. For example, nginx should serve an URI of the following pattern:

www.mysite.com/public/doc/foo/bar.html

with the .html file that is located at /home/www-data/mysite/public/doc/foo/bar.html. You can think of foo as the set name, and bar as the file name here.

I wonder whether the following piece of nginx config would do the job:

server {
    listen        8080;
    server_name   www.mysite.com mysite.com;
    error_log     /home/www-data/logs/nginx_www.error.log;
    error_page    404    /404.html;

    location /public/doc/ {
        autoindex         on;
        alias             /home/www-data/mysite/public/doc/;
    }

    location = /404.html {
        alias             /home/www-data/mysite/static/html/404.html;
    }
}

In other words, all requests of the pattern /public/doc/.../....html are going to be handled by nginx, and if any given URI is not found, a default www.mysite.com/404.html is returned.

asked Oct 9, 2012 at 19:17

MLister's user avatar

MListerMLister

10.3k18 gold badges67 silver badges93 bronze badges

It should work, however http://nginx.org/en/docs/http/ngx_http_core_module.html#alias says:

When location matches the last part of the directive’s value: it is better to use the root directive instead:

which would yield:

server {
  listen        8080;
  server_name   www.mysite.com mysite.com;
  error_log     /home/www-data/logs/nginx_www.error.log;
  error_page    404    /404.html;

  location /public/doc/ {
    autoindex on;
    root  /home/www-data/mysite;
  } 

  location = /404.html {
    root /home/www-data/mysite/static/html;
  }       
}

dipole_moment's user avatar

answered Oct 9, 2012 at 19:43

cobaco's user avatar

cobacocobaco

10.6k6 gold badges39 silver badges33 bronze badges

2

You can use the following code. You can also use this code to serve vuejs or reactjs static files, Just modifile the alias to point to the root of your file directory;

location /files {
    #autoindex on;
    alias  /files;
    try_files $uri /index.html =404;
  }

Make sure the access permissions are right too. sudo chown -R 1000 /files

Example on how to access the files http://example.com/files/text-file.txt

answered Mar 17, 2023 at 8:18

Jay's user avatar

JayJay

1,0623 gold badges21 silver badges35 bronze badges

if you are using static middleware for example app.use(express.static('assets'));

suppose you have an image in yourprojectAddress/assets/media/cat1.jpg and you are using port 3000 for your application

so to serve that image this is nginx config

location /media/ {

 proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

answered Jun 24, 2024 at 11:03

mohammad reza shabani's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.