Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I serve a specific file on my server as a specific path with nginx?

Tags:

nginx

I have a website example.com and I want to serve the following file

/home/myweb/path/file1.html

as

http://example.com/some/spec/path/

It means, when people visit http://example.com/some/spec/path/, they will see the content of the file1.html on that page.

How should I config it?

like image 784
AGamePlayer Avatar asked Oct 22 '25 13:10

AGamePlayer


1 Answers

One solution is to use a specific location to match the URL. The block then contains a root and try_files statement to describe the file to be returned.

For example:

location = /some/spec/path/ {
    root /home/myweb/path;
    try_files /file1.html =404;
}

The =404 will not be reached if the file exists, but is necessary as try_files requires at least two parameters and in this case /file1.html cannot be the last parameter.

See the location documentation and the try_files documentation.

like image 187
Richard Smith Avatar answered Oct 24 '25 19:10

Richard Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!