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
Related
This is my server block at /etc/nginx/sites-enabled/mydomain;
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name mydomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Inside my /var/www/html/ folder I have folder1 and folder2. I can access both of these at mydomain.com/folder1 and mydomain.com/folder2. folder1 is currently being served as mydomain.conf/folder1 so no issues on that one.
However, I want to server folder2 as my main domain. So when I access mydomain.com it should serve whatever insides /var/www/html/folder2.
I literally tried every single answer I could find online (and I'm aware there are dozens of similar questions online) yet none of them worked for me. Sorry I'm a bit rookie and I appreciate your understanding.
EDIT: Both folder1 and folder2 contain PHP apps.
Your first option is to use
root /var/www/html/folder2;
and put folder1 under the /var/www/html/folder2. If you can't do it for some reason, your second option is to use the following config (I omit the try_files directive from your root location since try_files $uri $uri/ =404 is a default behavior):
server {
listen 80;
root /var/www/html/folder2;
index index.php index.html index.htm index.nginx-debian.html;
server_name mydomain.com;
location / {}
location /folder1/ {
root /var/www/html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
If you want the /folder1 URL to be workable too, you either change location /folder1/ { ... } to location /folder1 { ... } or add an explicit redirect (this won't needed if you put your folder1 under the /var/www/html/folder2, in that case this redirect will be issued automatically):
location = /folder1 {
return 301 /folder1/$is_args$args;
}
I prefer the second way since using the first one you made any route started with /folder1 prefix (but different from it, e.g. /folder10) unavailable for your root web app.
I have an IP address of my server that I want to put my website Frontend and Backend admin. The site1 part is simply should be at "http://IP/" and and site2 should be in "http://IP/admin" .
I have installed Nginx in server and my websites files are inside: Lets say its like :
site1: /var/www/html/site1/index.html
site2: /var/www/html/site2/index.html
I created 2 files in /etc/nginx/site-available/ called "site1.conf" and "site2.conf" .
site1.conf:
server {
listen 80;
listen [::]:80;
root /var/www/html/site1;
index index.html index.htm;
server_name http://myIP;
location / {
try_files $uri $uri/ =404;
}
}
site2.conf:
server {
listen 80;
listen [::]:80;
server_name http://myIP;
location /admin {
autoindex on;
alias /var/www/html/site2;
try_files $uri $uri/ /index.html last;
index index.html;
}
}
Then I linked these 2 files into "/etc/nginx/site-enabled"
After restarting the Nginx, my "http://ip/" opens site1 "index.html" and works fine.
but "http://ip/admin/" gives 404 error instead of opening site2 "index.html"
http://IP/ and http://IP/admin both point to the same server, with the server_name "IP".
Your server contains at least two location blocks.
For example:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /admin {
alias /var/www/html/site2;
...
}
}
The server name only contains the text of the IP address or the DNS name. See this document for more.
You can spread your configuration across as many files as you choose. See the include directive.
The nginx configuration is a file called nginx.conf and contains an include statement to source all of the files in the sites-available directory. The content of these files are contained within the http { ... }.
As I have already stated, your two services are one server { ... } block, as far as nginx is concerned. However, you can still create a server block file in sites-available that includes files from some other location. Just don't use sites-avalable or conf.d, as nginx is aready using those directory names.
For example:
In sites-available/mysites.conf:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
include /path/to/my/location/confs/*.conf;
}
And in /path/to/my/location/confs/site1.conf:
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
And in /path/to/my/location/confs/site2.conf:
location /admin {
alias /var/www/html/site2;
...
}
I am not saying that this is a good way to organise your files, but with nginx, many things are possible.
I've tried this:http://www.tweaktalk.net/60/nginx-remove-php-file-extension-from-url but it didn't quite work for me. I'm getting either Error 404 or Error 505
I'm using Digital Ocean hosting with nginx server.
If someone wants to know how I solved this:
I've went to the directory /etc/nginx/conf.d , created there a folder called 'domain.trade.conf', for example if my domain is example.com, the folder name would be example.com.conf.
In this file I've added this code:
server {
listen 80;
server_name example.com; #Domain
root /var/www/html; #The place of your site files
access_log /var/www/html/logs/access.log; #Where to log the accesses
error_log /var/www/html/logs/error.log; #Where to log the errors
location / {
try_files $uri $uri/ #htmlext;
}
location ~ \.html$ {
try_files $uri =404;
}
location #htmlext {
rewrite ^(.*)$ $1.html last;
}
}
Hope I helped you.
I'm trying to upload a bunch of html and image files to my Nginx webserver which is running Ghost (the blogging platform) lets call it ghost-blog.com. Ghost runs perfectly fine, but additionaly I want serve other files and folders under the same domain e.g. ghost-blog.com/text.html and ghost-blog.com/subfolder/index.html.
After spending some time googling for an answer it seems I've bumped into something "new". I am aware I need to make changes to the /etc/nginx/sites-available/default file. What I don't know is what to add/edit so that
I create a /some/random/public folder public
This does not conflict with Ghost which is already serving content, specially the default index index.html index.htm files.
My current /etc/nginx/sites-available/default config file looks like this:
server {
listen 80;
server_name www.ghost-blog.com;
rewrite ^/(.*) http://ghost-blog.com/$1 permanent;
}
server {
root /usr/share/nginx/www;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
Any suggestions on how I could go around creating a /public folder serving other files and sub-folders?
server {
listen 80;
server_name www.ghost-blog.com/subfolder;
rewrite ^/(.*) http://ghost-blog.com/subfolder/$1 permanent;
}
server {
root /usr/share/nginx/www/NEWSITEFOLDER;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
then in ssh you need to make this directory to run the new ghost blog from
/usr/share/nginx/www/NEWSITEFOLDER;
so run command
mkdir /usr/share/nginx/www/NEWSITEFOLDER;
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