Question

About URL extensions at Nginx

Hello,

I have a droplet and a website. I can upload my html files to my droplet and see them correctly.

But I just want to see my web pages like this; 12.34.56.78***/home*** 12.34.56.78***/about*** 12.34.56.78***/test*** etc

and not like this; 12.34.56.78/home***.html*** 12.34.56.78/about***.asp*** 12.34.56.78/test***.php*** etc

How can i do that? Is that possible with nginx settings?

Thank you.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

You can set the default type to text/html. Keep in mind that this will serve every file that nginx doesn’t know the type for as html.

@Dexilez

It really depends on your application and how it’s configured to handle requests, more specifically:

  • REQUEST_URI
  • PATH_INFO

The most basic way to handle this would be to use try_files in the location block. Keep in mind, this will rewrite ALL URI’s and attempt to match each request to an existing HTML file. If that file does not exist, an error will result (more specifically, a 500 error). With this in mind, it’s important that you setup your error pages as well and not exclusively rely on this location block (example of this below as well).

Working Example

location / {
    try_files $uri $uri/ $uri.html;
}

This will allow:

… where anyactualfile = anyactualfile.html.

To setup the error files, you’d place the following above your location block:

error_page 404 /error_404.html;
error_page 500 /error_500.html;
error_page 503 /error_503.html;
...
...
...

resulting in:

error_page 404 /error_404.html;
error_page 500 /error_500.html;
error_page 503 /error_503.html;

location / {
    try_files $uri $uri/ $uri.html;
}

Relying on this type of rewrite also affects directories, too. So if a request comes through such as:

… and the directory /me/ does not exist, an error will result, so you’ll need to be able to handle them by creating potential the directories.

Thanks alot guys!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more