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

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?

Related

Without specifying a port number inside of a API request how server decided that this request is for port 80 or 443

I did not mention port number in my api end point, but how server decided to serve this request using port 80 or 443
const {data} = await axios.get('/api/users/currentuser');
Use something like this in NGINX:
server {
root /var/www/html;
server_name _;
# this is the react/angular fronted application endpoints
location / {
try_files $uri $uri/ /index.html;
}
# this is the api endpoints
location /api {
proxy_pass http://127.0.0.1:8000;
}
}
However you have not explicitely mentioned in the question that you are using NGINX or not but you have nginx in one of the 5 tags of this question, so I am supposing that you must be using NGINX and so, I am answering in that context.

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?

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

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;
}
}

nginx: proxy_pass subdirectories to other servers

Im using nginx as Web Server and Reverse Proxy with SSL enabled.
The Web Server serves an WoltLab Suite Forum 5.0.0 (formerly known as Burning Board) and proxied some subdomains to different hosts like a NodeJS backend, an Tomcat backend and many other different services.
This worked great so far, but now i have the problem that i can no longer use subdomains to accomplish this.
Please don't ask why, please don't.
Now, that i can no longer use subdomains, Im trying to get it working with sub directories.
An example:
I had xyz.example.com point to my nginx server at 12.13.14.15.
nginx proxied all requests to xyz.example.com to 10.20.30.40:1234
Now i want nginx to proxy all requests to example.com/xyz/ to 10.20.30.40:1234
I got this working with Apache Archiva as backend service, but all other services like my NodeJS backend are refusing to work correctly with my current configuration.
It sends me to the BurningBoard wich shows me their Page-Not-Found page.
example.com/xyz/admin/index.php becomes to example.com/admin/index.php wich wont work of course.
The directory that proxies to Archiva has the exact same configuration, just other directory names of course.
The Archiva URL looks like this after i call it from Web:
example.com/repo/ becomes example.com/repo/#welcome and shows me Archivas Welcome Page.
This is exactly what i want for my other services too.
Here are my current configuration files for nginx (sensitive data replaced with X):
<=== sites-available/default ===>
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name XXXX.XX www.XXXX.XX;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-XXXXX.XX.conf;
include snippets/ssl-params.conf;
root /var/www/html;
include /etc/nginx/snippets/proxies.conf;
# the last try_files argument is for SEO friendly URLs for the WSF 5.0
location / {
index index.php;
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# Lets Encrypt automation
location ~ /.well-known {
allow all;
}
location ~ /\.ht {
deny all;
}
}
<=== snippets/proxies.conf ===>
# Apache Archiva
location /repo {
rewrite ^/repo(/.*)$ $1 break;
proxy_pass http://XXXXXXXX:XXXXX;
return 404;
}
# Git Solution
location /git {
rewrite ^/git(/.*)$ $1 break;
proxy_pass http://XXXXXXXX:XXXXX;
return 404;
}
# Filehosting
location /cloud {
rewrite ^/cloud(/.*)$ $1 break;
proxy_pass http://XXXXXXXX:XXXXX;
return 404;
}
# NodeJS
location /webinterface {
rewrite ^/webinterface(/.*)$ $1 break;
proxy_pass https://XXXXXXXX:XXXXX;
include /etc/nginx/snippets/websocket-magic.conf;
return 404;
}
Any ideas how to solve this problem?
Also, please tell me if you need more informations like nginx' version or the like.