Nginx static file serving confusion with dynimac alias - nginx

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?

Related

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; }

nginx does not serve to non-www domain

I have the following nginx configuration, which is supposed to serve the index.html file to both www.domain.com and domain.com. While www.domain.com works, domain.com returns the default nginx welcome page.
What am I doing wrong?
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
root /home/ubuntu/coming-soon-page/frontend/react_ui/build;
location = /favicon.ico {
access_log off; log_not_found off;
}
error_page 403 /home/ubuntu/coming-soon-page/frontend/react_ui/build/index.html;
location / {
root /home/ubuntu/coming-soon-page/frontend/react_ui/build/;
index index.html;
try_files $uri $uri/ /index.html;
}
location = /submit_user_feedback {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/coming-soon-page/backend/api/api.sock;
}
}

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

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.

Host multipe sites on nginx

Currently my nginx.conf looks like this:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/html;
index index.html index.htm;
# 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;
}
# Attempt to load static files, if not found route to #rootfiles
location ~ (.+)\.(html|json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {
try_files $uri #rootfiles;
}
# deny access to . files, for security
#
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location #rootfiles {
rewrite ^/(?:triangles|equation-of-a-line)/(.*) /$1 redirect;
}
sendfile off;
}
I basically have one site at root /usr/share/nginx/html;.
How can I change this to allow multiple sites with each site hosted at the same root directory?
Also, I have cname * mapped to the IP address.
How can I change this so that each website has its own cname?

Nginx 500 error page is not working with matches any unmatched URL

In my Nginx conf. error 500 rule is not working with location conf. lines below.
location / {
try_files $uri $uri/ #rewrite;
}
error_page 500 /error.html;
location = /error.html {
root /path/to/nginx;
internal;
}
location #rewrite{
rewrite ^ /index.php?q=$uri;
}
The error.html file is in /path/to/nginx directory.
When I create a 500 error in the code, it couldn't run the Nginx 500 rule.
If I remove the location / { part, and change 500 to 404, it works.
Edit:
server {
listen 80;
listen 443 ssl;
server_name_in_redirect off;
port_in_redirect off;
server_tokens off;
server_name www-dev.somc.io;
root /path/to/site;
index index.php index.html index;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
error_page 500 /error.html;
location = /error.html {
root /usr/share/nginx/html;
internal;
}
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite{
rewrite ^ /index.php?q=$uri;
}
add_header Cache-Control no-cache;
set $skip_cache 0;
set $cache_control max-age=660;
location ~ \.php$ {
include /etc/nginx/php-handler.conf;
fastcgi_pass hhvm;
}
}

Resources