Can i match www folder structure in nginx? - nginx

In my www root folder I have folder app1, app2, app3 and so on. can I match nginx configuration to match the folder with the /appName? for instance 192.168.10.116 should go to the default nginx folder that was set up (nginx Hello world). However 192.168.10.116/app1 should go to app1. In /var/www/app1 I have index.html
server {
listen 80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name 192.168.10.116;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/app1;
index index.html;
server_name 192.168.10.116/app1;
location / {
try_files $uri $uri/ =404;
}
}

Related

How to configure multiple react projects using nginx?

I have 3 different React projects, pointing to the same IP address with different ports.
Routing works accurately for the first project(default project)
For the other 2, routing works fine if I'm navigating from the very first page of the website.
For an instance, if I'm at some.ip:3000 then I click something and now, I'm at some.ip:3000/page, it works fine
but if I try some.ip:3000/page directly, 404 page is returned.
Following is the nginx configuration - /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name <private-IP>;
root /var/www/<project1>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}
server {
listen 3000;
listen [::]:3000;
server_name <private-IP>;
root /var/www/<project2>;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8000;
listen [::]:8000;
server_name <private-IP>;
root /var/www/<project3>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}

nginx: how to use multiple roots for multiple locations, including / ? (what's wrong with my paths config?)

I'm trying to setup nginx for first test uses, without a domain yet.
My current goal is to show some page at http://<server IP> and serve some static content at http://<server IP>/projectname. The "some page" is currently just the default /var/www/html/index.nginx-debian.html.
In /etc/nginx/sites-available/ I've created a projectname config and I've put a link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/tiddlywiki /etc/nginx/sites-enabled/
The first version of config was
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
}
What I got, is http://<server IP> started serving static files, but http://<server IP>/projectname showed 404. How do I fix that? Because next step, I've followed this answer and tried to set 2 locations:
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
but only got the default page at http://<server IP> back again, and 404 at http://<server IP>/projectname.
Ok, so the problem was, with root directive, path is concatenated to the root, so with this config
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
nginx tried to serve /projectname → /some/path/to/project/static-files/projectname which is an unexisting folder (existing one is /some/path/to/project/static-files). What I needed is the alias directive:
server {
listen 80;
listen [::]:80;
server_name <server IP>;
index index.html;
location /projectname {
alias /some/path/to/project/static-files;
index index.html;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
I'm not sure how exactly try_files works so I've removed it for now and also added the index directive.

Content linked to domain is not working but with subdomain it is working on nginx

I have created several subdomain so that i can serve the subdomain related specific content. But the problem i am facing is that when i am trying to access the content related directly to my domain am not getting anything in return in nginx. Below is my configuration file
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/myfile/dev/web;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name dev.example.com;
root /usr/share/nginx/myfile/dev/mobile;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa-crm.example.com;
root /usr/share/nginx/myfile/qa-crm;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa.example.com;
root /usr/share/nginx/myfile/qa;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
}
All server block are serving perfectly except the first one which contains the main domain name i.e. (example.com www.example.com)
No idea what i am doing wrong here. I am new to nginx world

How to configure Nginx to change root directory on existence of a particular cookie?

Trying to configure my site to change root directory when there is a cookie var called "developer". This is on a Debian server.
This is my current site config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /sites/live/;
if ($http_cookie ~ 'developer') {
root /sites/dev/;
}
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
But that gives me this error:
nginx: [emerg] "root" directive is not allowed here
What should I be doing? Thanks!
add root inside the location and the condition what you want
following is he my configuration working fine
location / {
root /var/www/myside;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
if that not work use rewrite rule

Nginx redirection from any subfolder to root index.html

Anything put as a subfolder, it will read the domain.com/index.html
Similar to this but in Nginx
Redirect URL non existent subfolder to root
server {
listen 80;
listen [::]:80;
server_name example.com;
root /apt/website/path;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

Resources