trynacode
trynacode

Reputation: 371

NGINX location configuration

Right now I have set up NGINX and had a question regarding the url paths and how NGINX approaches it. Right now, if I do something like /sample.html, it will effectively display sample.html. However, I want to do something where irregardless of what the user inputs in the URL, it displays sample.html. For example, doing something like /network will still display sample.html but will retain the url path of /network. Right now, this is how my NGINX.conf looks like:

 worker_processes  2;

 pid         logs/nginx.pid;
 error_log   syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=error;

 events {
     worker_connections  1024;
 }


 http {
     include            mime.types;
     default_type       application/octet-stream;

     access_log    syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=info combined;

     sendfile           on;
     keepalive_timeout  65;

     server {
         listen         80;
         server_name    localhost;
         root           /home/user/Downloads/nginx/html;

         location /network {
             index      sample.html sample.htm;
         }

         location / {
             index      sample.html sample.htm;
             autoindex  on;
         }  

         #error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root       html;
         }
     }
 }

I thought this would work but it is just returning a 404 error. Any idea on how I can change this so that it would work in how I want it to work, if it is possible?

Thank you, and let me know if you need more information!

Upvotes: 0

Views: 93

Answers (1)

Jchieppa
Jchieppa

Reputation: 126

You could use try_files to accomplish this within a location block. See http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

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

Upvotes: 2

Related Questions