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?
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;
}
}
I want to deny access to few directories, as well as all sub-directories and files, including JS/CSS files.
I have this configuration, and it works for the most part, but it doesn't deny access to .js file I have.
server {
listen 80;
server_name DOMAIN www.DOMAIN;
root /home/me/www/app;
index index.php index.html index.htm;
autoindex on;
client_max_body_size 20m;
fastcgi_read_timeout 600;
#Forbid access to these directories
location ~ /(data|dev|py)/ {
deny all;
return 403;
}
#Force download on PDF files
location ~* /(.*\.pdf) {
types { application/octet-stream .pdf; }
default_type application/octet-stream;
}
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
#API
location /api/v1/ {
index index.php;
try_files $uri $uri/ /api/v1/index.php?$args;
}
#php support
location ~ [^/]\.php(/|$) {
include /etc/nginx/conf.d/php_generic;
fastcgi_param DOCUMENT_ROOT /home/me/www/site1;
fastcgi_pass unix:/var/run/php/php-fpm-me.sock;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
error_log /home/me/logs/error.log;
access_log /home/me/logs/access.log;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/DOMAIN/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/DOMAIN/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
Any ideas?
EDIT / RESOLVED
Well, the website is behind CloudFlare, so after 30 minutes of pulling my hair off, link is no longer accessible, which tells me that CloudFlare had it cached and served it, even tho nginx config was changed.
I config a static site via nginx, and include my settings under conf.d directory. listen port 80.
But I found when I request the url, nginx always redirect the default page /usr/share/nginx/html/index.html
My configuration does seem work, for all access logs were written to my access log settings, and if I change index.html to some other name(i.html for example) in my directory, and request url mysite.com/i.html, I can access the correct page.
So, it seems that nginx redirect all index.html to the default one.
I tried change default port/server name/root and annotate the default settings, even close selinux, all above doesn't work, it really make me mad.
Can anyone help me?
I'm using nginx version 1.10.2, on CentOS 7.2
and following is my site settings:
# the upstream component nginx needs to connect to
upstream blog {
server 0.0.0.0:80; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# the domain name it will serve for
server_name mysite.com;
charset utf-8;
access_log /var/log/blog/access.log;
error_log /var/log/blog/error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / { try_files $uri #blog; }
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
}
and the /etc/nginx/nginx.conf is
server {
# listen 8000 default_server;
# listen [::]:8000 default_server;
# server_name not;
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
This makes no sense:
location / {
try_files $uri #blog;
}
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
The lack of a root directive means that the first try_files will look for files in the default location.
All you need is root and index directives within the server context:
For example:
root /home/work/projects/blog/public;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
See this document for details.
Here are my nginx configure files.
On the default.conf, the first location is used to access /usr/share/nginx/html directory, it is ok while I access http://47.91.152.99.
But when I add up a new location for directory /usr/share/nginx/public directory, nginx return me a 404 page while I access http://47.91.152.99/test.
So, what is the matter? Am I misuse the directive of nginx?
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /test/ {
root /usr/share/nginx/public;
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;
}
}
The following erroneous block (in your case);
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
is telling nginx to look for directory 'test' into the folder (root) /usr/share/nginx/public. If there's no 'test' folder in that root, it will return 404. To paliate to your problem, i suggest you try using alias instead of root. Like so:
location ^~ /test/ {
alias /usr/share/nginx/public;
index index.html index.htm;
}
Also, just for kicks, index directive can be set generally so you don't have to re-write it all the time... like so;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location / { }
location ~^/test/ {
alias /usr/share/nginx/public;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
One thing you should also consider... the more 'precise' the location block, the higher in your config it should reside. Like that location = /50x.html. In a perfect world, that would be set up top, right after the general server block settings.
Hope it helps.
Error caused by root directive
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
Fix with alias directive
location ^~ /test/ {
alias /usr/share/nginx/public;
index index.html index.htm;
}
Other Improvements
Extra tip: the index directive can be set so that you don't have to re-write it.
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location / { }
location ~^/test/ {
alias /usr/share/nginx/public;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx matches Location blocks partly based on position in the config. Ideally, you would invert what you have now. The location block would be higher in nginx config. To that end, the location = /50x.html would also move up. Order is
Exact match =
Forward match ^~ /
Case sensitive regex ~ /
Case insensitive regex ~*
Path match /
More about nginx location priority. Also, you can always review the official documentation. The nginx documentation for location block http://nginx.org/en/docs/http/ngx_http_core_module.html#location
when your app is vuejs,you need write like this,can prevent 404,pay attention to double /test/
location ^~/test/ {
alias /usr/local/soft/vuejs/;
try_files $uri $uri/ /test/index.html;
}
I just solved this (index.html not found) issue.
For me, I misstyped my project name to match your ec2 project name with the nginx path.
Move to nginx/sites-enabled to check nginx path
cd /etc/nginx/sites-enabled
cat your_project_name
Check your nginx path
(for me : root/home/ubuntu/practice/current/public;)
Move to home directory to check your project name
4. cd
5. ls
6. If your ec2 project name(for me: practice) is not match with your nginx path name(for me: practice) then you might got "index.html not found error"
and my server-blocks.conf
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
server_name 13.xxx.xxx.xx;
location / {
root /var/www/portaladmin/;
proxy_pass http://13.xxx.xxx.xx:80/;
}
error_log /var/log/nginx/portaladmin-erorr.log;
}
and my load-balancer.conf
server {
listen 80;
server_name xx.xxx.xxx.xx
access_log /home/ec2-user/logs/lb-access.log;
error_log /home/ec2-user/logs/lb-error.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://13.xxx.xxx.xx:80;
}
}
Creating nginx & sftp pods using Kubernetes.. I've found that the mountPath in my sftp-deployment.yaml file is related to the username in my sftp-server.
And the error has happened when I have changed the username without changing mountPath value to match my username. So, the files were uploading to '/home/old-username' instead of '/home/new-username'.