I have some static html files under:
/var/www/project1
Nginx config for this project is:
server_name www.project1.com project1.com;
root /var/www/project1;
location / {
index index.html;
}
My goal is to use nginx so that when a user enters this url:
www.project1.com/project2
Nginx uses another root, I have tried:
location /project2 {
root /var/www/project2;
index index.html;
}
But this is not working. Any idea on how to achieve this?
According to your config of project2
location /project2 {
root /var/www/project2;
index index.html;
}
Nginx will be looking for files under the path /var/www/project2/project2/ for your requests to project2. So if your project2 is under /var/www/project2, The correct config should be
location /project2 {
root /var/www;
index index.html;
}
Another alternative is to use alias instead of root.
in your case is alias /var/www/project2, check here
Related
I use docker container to serve an html and some other files. there is no back-end website. I just server some static files.
I need to point some different url to one folder.
I have one index.html file in /usr/share/nginx/html.
I want to point all ulr like:
/ , /a , /a/b/c , /a/
to the same:
/usr/share/nginx/html/index.html file.
my nginx config is as below :
server {
listen 80;
location /lalala/ {
proxy_pass http://lalalalalala/;
}
location /proxy/bebebebe/ {
proxy_pass https://www.bebebebe.com/;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
I try to use alias like this:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
alias /a /usr/share/nginx/html
alias /b /usr/share/nginx/html
alias /a/b/c /usr/share/nginx/html
}
But it's not work.
You can use try_files to look for a specific file and return index.html if that file is not found.
For example:
location / {
root /usr/share/nginx/html;
try_files $uri /index.html;
}
See this document for details.
This works fine by accessing http://localhost
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
But why doesn't it work when I try to access http://localhost/test with this configuration?
location /test {
root /usr/share/nginx/html;
index index.html index.htm;
}
Use the alias directive:
location /test {
alias /usr/share/nginx/html;
index index.html index.htm;
}
With the root directive, the value of the root and the URI are appended together to obtain the path to the file.
With the alias directive, the value of the location is removed from the URI first, so /test/index.html will be mapped to /usr/share/nginx/html/index.html.
See this document for details.
How I configure a single folder with name "projects", where each subfolder is a subdomain?
Example:
I have site example.com
On my server I have folder
/var/www/html/example.com
I created new folder:
/var/www/html/projects/
And every folder in 'projects' directory is new subdomain:
/var/www/html/projects/site1 = site1.exapmle.com
Thanks
#richard-smith, I set config file projects in /etc/nginx/sites-available with content:
server {
listen 80;
root /var/www/html/projects/$domain;
index index.php index.html index.htm index.nginx-debian.html;
server_name ~^(www\.)?(?<domain>\.example\.com)$;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
//instead example.com my domen name.
Created symbol link in /etc/nginx/sites-enabled and run service nginx reload.
But after open site1.example.com. I get "This site can’t be reached". But my example.com work fine.
Using a regular expression in the server_name directive will allow you to capture the name of the subdomain and use it in the root directive.
server {
server_name ~^(www\.)?(?<domain>\.example\.com)$;
root /var/www/html/projects/$domain;
...
}
See this document for more.
In my case, I wanted to match subdomains excluding 'www', with folder names being just the subdomain name, and this syntax worked for me.
server {
...
server_name ~^(?!www)(?P<domain>.+)\.example\.com$;
root /var/www/html/projects/$domain;
...
}
Where test.example.com will use root location /var/www/html/projects/test
This seems very basic, but I am not able to solve this.
I want NGINX to serve source urls outside the current root, basically starting with ../
This is my directory
common
root (NGINX root)
->index.html (NGINX default index)
common (another common folder)
I want to be able to serve
src="../common/whatever" /*outer common folder*/
src="./common" /*inner common folder; Cannot change unfortunately*/
As you can see, simple location /common will not work.
This is my current NGINX conf,
server
{
listen 83 default_server;
listen [::]:83 default_server ipv6only=on;
root C:/www/root;
index index.html index.htm;
#This does not work for inner /common folder
location /common {
root C:/www;
try_files $uri /index.html;
}
location / {
try_files $uri /index.html;
}
}
Maybe you can do the reverse:
root/
common/*
site/
site/common/*
and do something like
location /common {
try_files site/$uri $uri ...;
}
etc... which gives you $root/site/common/FILE, $root/common/FILE....
I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com/static;
}
}
In this example going to test.example.com/ would bring the index file in /web/test.example.com/www
and going to test.example.com/static would bring the index file in /web/test.example.com/static
You need to use the alias directive for location /static:
server {
index index.html;
server_name test.example.com;
root /web/test.example.com/www;
location /static/ {
alias /web/test.example.com/static/;
}
}
The nginx wiki explains the difference between root and alias better than I can:
Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.
Note that root and alias handle trailing slashes differently.
The Location directive system is
Like you want to forward all request which start /static and your data present in /var/www/static
So a simple method is separated last folder from full path , that means
Full path : /var/www/static
Last Path : /static and First path : /var/www
location <lastPath> {
root <FirstPath>;
}
So lets see what you did mistake and what is your solutions
Your Mistake :
location /static {
root /web/test.example.com/static;
}
Your Solutions :
location /static {
root /web/test.example.com;
}
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com;
}
}
https://nginx.org/en/docs/http/ngx_http_core_module.html#root
A little more elaborate example.
Setup: You have a website at example.com and you have a web app at example.com/webapp
...
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html/website_dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location /webapp/ {
alias /usr/share/nginx/html/webapp_dir/;
index index.html index.htm;
try_files $uri $uri/ /webapp/index.html;
}
}
...
I've named webapp_dir and website_dir on purpose. If you have matching names and folders you can use the root directive.
This setup works and is tested with Docker.
NB!!! Be careful with the slashes. Put them exactly as in the example.
If you use this, I will suggest you set up this command too.
location /static/ {
proxy_set_header Host $host/static; // if you change the directory and the browser can't find your path
alias /web/test.example.com/static/;
}
If you want to check two different directories for the same URI use this config:
server {
...
root /var/www/my-site/public/;
...
index index.php index.html index.htm;
...
location / {
root /var/www/old-site/dist/;
try_files $uri $uri/ /index.php$is_args$args;
}
...
}
If Nginx couldn't find file in /var/www/old-site/dist/ directory, then it will try file in /var/www/my-site/public/ directory, but as we said to Nginx to try files with $uri $uri/ /index.php$is_args$args patterns, so Nginx will try /index.php$is_args$args in /var/www/my-site/public/ directory. not $uri
If you want to complete your fallthrough, then replace /index.php$is_args$args with /fallthrough$uri and then add the location /fallthrough { ... } with the alias key to your target directory.
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block