Try to serve static index.html with NGINX giving 404 error - nginx

My NGINX conf file looks like this:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name site.com;
root /root/mysite;
resolver 8.8.8.8;
location / {
try_files $uri /index.html =404;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
This configuration gives me 404 error. But why? This directive try_files $uri /index.html =404; should try file and if not exist fallback to 404 error.
But I do have a file in root directory.

Related

Set nginx to only monitor subdirectory

I'm trying to change nginx config file so that nginx only monitors localhost/test and stops monitoring localhost/.
I tried replacing the default file in /etc/nginx/conf.d/default.conf but I'm getting 404. I basically just changed location / { to location /test {
server {
listen 80;
listen [::]:80;
server_name localhost;
location /test {
alias /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I'm unsure why this is not working. I also tried to change listen [::]:80; to listen [::]:80/test; but there I got an error invalid host in "80/test" of the "listen" directive
Edit:
I tried copying the index.html to /usr/share/nginx/html/test/ and changed alias in location to root but then when I navigated to localhost/test I got 403 Forbidden

Nginx static file serving confusion with dynimac alias

I have web resources in /usr/share/nginx/html/act/h5-demo, content list below:
index.html
index.css
index.js
my default.conf:
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html/;
access_log /var/log/nginx/host.access.log main;
location ~* \.(js|css|jpe?g|png)$ {
add_header xF-NGINX-request_uri $request_uri;
expires 168h;
}
location / {
if ($request_filename ~* .*.(html|htm)$)
{
add_header Cache-Control "no-cache";
}
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Now I can visit the resource by url: localhost:80/act/h5-demo/, It works well.
But I want to visit the resource by: localhost:80/act/h5-demo-*/, the "*" reprents any number (1,2,3...). For example: localhost:80/act/h5-demo-10/ .
How can I modify the configuration file?

NGINX 502 response after applying config

I have a dockerized app running on nginx. With the default config every time I reload the app and I'm not on the / route, I get a 404 Not Found error. I also want to always redirect my http traffic to https, so I put this config together.
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
return 301 https://$host$request_uri;
}
But when I apply the config I always get The server encountered a temporary error and could not complete your request. Please try again in 30 seconds

Nginx redirect to 404 page if there is a trailing slash at the end of the URL

I got a ask of doing a redirect to 404 page if there is a trailing slash at the end of the URL.
Eg:
https://www.example.com/test/
If the user enters a trailing slash at the end means we need to redirect it to 404 page. Previously I have redirected the same URL by removing the trailing slash, Now the requirement has changed to redirect to the 404 page.
Here is the nginx conf file for it. Please let me know what change I have to make here to achieve the scenario.
nginx.conf
server {
listen 80;
rewrite ^/(.*)/$ /$1 permanent;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Updated Conf:
Default Nginx 404 error page is displayed after this config. What's the update required here to display the custom 404 page?.
server {
listen 80;
location ~ ^/(.*)/$ {
return 404;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Remove the rewrite ^/(.*)/$ /$1 permanent; statement and replace it with:
location ~ ^/(.*)/$ { return 404; }

Invalid url prefix nginx

Hi I am trying to start nginx with this config.
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /graphql/ {
proxy_pass http://localhost:18000/graphql;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
But I am getting this error
2019/09/06 14:23:22 [emerg] 1#1: invalid URL prefix in /etc/nginx/conf.d/nginx.conf:11
nginx: [emerg] invalid URL prefix in /etc/nginx/conf.d/nginx.conf:11
I am not sure what I am missing?

Resources