How To Set Up common one Nginx Server Blocks (Virtual Hosts) for all peojects in Centos - apache

I have a Centos with Nginx server and multiple site folders are exist in wamp.
But for every project i should need to write separate Nginx server blocks under /etc/nginx/conf.d/websites.conf file. So whenever i created a new project then after i have to add below lines under websites.conf file of Nginx.
location /project-folder {
root path;
index index.php index.html index.htm;
rewrite ^/project-folder/(.*)$ /project-folder/app/webroot/$1 break;
try_files $uri $uri/ /project-folder/app/webroot/index.php?q=$uri&$args;
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:xxxx;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* /project-folder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
root path/project-folder/app/webroot/;
try_files /$1.$2 =404;
}
}
So is it any other way to make a common block for all site-folder and doesn't need to add new server block for new site?
Thanks in advance.

There are multiple ways to implement this. If you are using multiple domain names, you can use a regular expression in the server_name to create named captures (see this document for more). You can use a regular expression in the location directive to capture the value of project-folder (see this document for more).
The main function of this configuration is to insert the text /app/webroot between the project name and the remainder of the URI. The challenge is to do it without creating a redirection loop.
I have tested the following example, which works by placing a generalised version of your rewrite statement into the server block and capturing the project name for use later in the one of the try_files statements:
server {
...
root /path;
index index.php index.html index.htm;
rewrite ^(?<project>/[^/]+)(/.*)$ $1/app/webroot$2;
location / {
try_files $uri $uri/ $project/index.php?q=$uri&$args;
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:xxxx;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ~* \.(css|js|ico|gif|png|jpg|jpeg)$ {
try_files $uri =404;
}
}

Related

Nginx - how to block endpoint by redirect to vue 403 page

Have a problem with vue/quasar framework with nginx as a host.
my standard endpoint looks like: exmaple.com/#/
I want to block exmaple.com/#/test by rerout this to 403. So in my nginx.conf I added like below:
server {
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html$is_args$args;
}
location ^~ /test {
deny all;
}
}
When I go to exmaple.com/#/test it simply passes me. But when I go to exmaple.com/test I get the 403. So it seems that the issue related to vue.js setup?
How can I resolve this?

Nginx try_files points to wrong folder

I'm hosting a vue history site using nginx docker and the nginx config is the following
server {
listen 80;
location / {
root /var/www/dist/;
index index.html index.htm;
try_files $uri $uri/ index.html;
}
}
I can access "http://the.ip.of.server". However when I tried to visit "http://the.ip.of.server/search", it shows the following error. It tries to open "/etc/nginx/htmlindex.html" instead of "/var/www/dist/index.html" which makes no sense.
2020/05/10 04:24:01 [error] 7#7: *2 open() "/etc/nginx/htmlindex.html" failed (2: No such file or directory),
client: xx.xx.xx.xx, server: , request: "GET /search HTTP/1.1", host: "the.ip.of.server"
I have no idea where "/etc/nginx/htmlindex.html" comes. The configuration should be right since I can access /.
You should put the root element outside the location block.
server {
listen 80;
root /var/www/dist;
index index.html index.htm
location / {
try_files $uri $uri/ =404;
}
}
After change the configuration remember to restart your nginx instance (if using docker force build image).

How to enable linked file in nginx proxy_pass sites?

I have a server, and 2 Express-based project running on port 3000 and 4000. Landing page has default template nginx html code, with Botkit iframe embed code. Port 3000 Express server is Botkit Starter Guide project, running with no modification. Port 4000 Express server is just Hello World project. Both server executed using pm2.
Below is my /etc/nginx/sites-enabled/default config:
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
return 301 http://hwsrv-492795.hostwindsdns.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name hwsrv-492795.hostwindsdns.com;
location / {
try_files $uri $uri/ =404;
}
location /test01/ {
proxy_pass http://142.11.241.150:3000/;
}
location /test02/ {
proxy_pass http://142.11.241.150:4000/;
}
}
So here is what I can't solve:
Botkit Chatbot is successfully loaded on http://hwsrv-492795.hostwindsdns.com.
While Botkit Chatbot behaves normally on http://142.11.241.150:3000/, it doesn't do so on http://hwsrv-492795.hostwindsdns.com/test01/. It just load /index.html, but failed (404) to load /css/styles.css, /embed.js, and /chat.html
Hello world behaves just fine on both http://142.11.241.150:4000/ and http://hwsrv-492795.hostwindsdns.com/test02/.
I can curl those file from the server terminal, meaning that there is no problem on accessing those file. The question is, how do I enable or allow linked files and folder for sites that is generated using Express server to be readable on browser?

Nginx: Basic auth protecting folders and PHP files, but not images

I have the following Nginx config
location ^~ /members {
auth_basic "Members Area";
auth_basic_user_file /home/*user*/public/*site*/www/members/.htpasswd;
location ~ \.php$ {
include /data/config/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Set header expirations for static content
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff|mp4|flv)$ {
access_log off;
log_not_found off;
expires 365d;
}
}
with the intention to password the /members/ directory, and all content within (.php, .jpg, .mp4 etc).
In it's current state, it prompts the user with the user/pass box as intended for .php files, however it doesn't fire the auth for the jpg images.
For clarity, the following behaviour is occuring:
http://site/members - user prompted for user/pass
http://site/members/category.php - user prompted for user/pass
http://site/members/content/upload/example.jpg - user able to see content without login
The /members/content folder is a symlink to a folder outside of the document root folder, which I wondered whether this was a potential cause.
I have tried a host of different configs, such as removing the ^~ on the /members block, but just can't get a working config to cover all files within the directory.
Any help would be greatly appreciated.
I think all you need is to define a new block to capture the images inside the members directory
location ~ ^/members.*\.(?:ico|css|js|jpe?g|JPG|png|svg|woff|mp4|flv)$ {
auth_basic "Members Area";
auth_basic_user_file /home/*user*/public/*site*/www/members/.htpasswd;
}

nginx - How do I make server-wide configuration for every hosted website

For each website I host with nginx, I have got a different file which holds the server {} block in /etc/nginx/conf.d/
For example...
/etc/nginx/conf.d/website1.co.uk
/etc/nginx/conf.d/website2.org
/etc/nginx/conf.d/website3.com
I find myself repeating the same code in every server {} block and was wondering if it is possible to make a "catch all" server {} block to house the reusable code.
This new "catch all" file would include things such as...
# Redirect all www. attempts to non-www.
server {
server_name www.$anything; hmm?
return 301 $scheme://$hostname$request_uri;
}
server {
server_name _; hmm?
# Add expires to static files
location ~* \.(?:ico|css|js|gif|jpe?g|png|bmp)$ {
expires max;
access_log off;
}
# Pass PHP files to PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Has anyone does this before?
Do we have to repeat these types of generic codes for each website we host?
All you need is include. Put all boilerplate in separate files, without server blocks. include fastcgi_params mentioned in the sample config is a prime example.
Without a leading slash, nginx will look in the directory where the main configuration file is. So:
include fastcgi_params;
include /etc/nginx/fastcgi_params;
are equivalent, if nginx.conf is in /etc/nginx.