How to route different webservers to different URL using nginx - nginx

creating a website for my self and need to host projects.
Basically, i hhave different projects with different framework. ie, Flask, Django, Node.JS and some html file projects. I would like to host them at projects.domain.com/<project name>
I tried to set server_name projects.domain.com/asdf but in error.log it says, server name "projects.domain.com/asdf" has suspicious symbols
Next up, i tried to nest location blocks (which i presume isn't how its supposed to be)
location /asdf {
location /static/ {
root blah blah;
}
location / {
..
proxy_pass http://localhost:3000 ;
}
}
But, this errors out saying location static is outside asdf
Some suggested to alias instead of root in the location /static/ block, but that doesnt work too.
Any help is appreciated :)

First of all a server_name can not contain URI segments. So a hostname or IP should be used as a value.
If you want to mix different local directories and proxy-locations a configuration could look like this.
Notice: Your location URI (/one, /two) will be appended to the root path.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not be part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
server {
server_name project.domain.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /one/ {
alias/var/www/html/project1/;
index index.html;
}
location /two/ {
alias/var/www/html/project2/;
index index.html;
}
}

Related

Nginx can't find file

I have simple nginx server configuration which works and looks like this:
server {
listen 80;
server_name _;
location /cert {
alias /aaa.txt;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Proxy pass works like it should do but what doesn't work is getting this /aaa.txt file.
I tried changing alias... into:
return 200 'Hi' - This indeed return and download file named Hi
root / > As I read, root just adds to link before so it is not suitable for this situation but tried it
try_files /aaa.txt =404 - Not working
I tried changing permission of /aaa.txt with command chown www-data:www-data aaa.txt and still not working.
I have tried creating folder /wwwroot/ and put it inside that folder and do everything the same but still not working.
I do not know what else to try.
Pointing to answers provided in suggested question Nginx return file for path I edited my setup file and (this time I put it in folder wwwroot) and it is still not working
GNU nano 6.2 /etc/nginx/sites-available/reverse-proxy
server {
listen 80;
server_name _;
location /cert {
index aaa.txt;
alias /wwwroot;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Solution was quite simple. I was all time putting path relative to ~ which is not where nginx starts. In terminal you see it as ~ but its full path is /home/[user]/ so when i created folder wwwroot it was not located at /wwwroot but /home/ubuntu/wwwroot and that is the reason why it coulnd't find it.

Nginx alias still points to and loads from root directory

I have a django backend and react frontend.
I want to serve the react on / and use /admin, /api and /auth for Django. Here's what I have in my Nginx.
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/frontend;
index index.html index.htm;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
With the above, the expected behavior is
/ uses the default root folder, /home/user/folder/frontend and loads the built index files from react accordingly
/(admin|api|auth) points to django
/static loads static files saved in the /home/user/folder/backend/staticfiles folder.
So not sure why when I hit example.com/static/myfile.css, Nginx is going to /home/user/folder/frontend/static/myfile.css
I'd expect none of the above configuration says that's what it should do, so what magic is going on?
I thought this answer was self explanatory enough, yet Nginx keeps doing whatever it likes.
I'm using nginx/1.18.0 (if that matters)
Try adding root inside the location / directive too.
Like this:
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/backend/staticfiles;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
root /home/user/folder/frontend;
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
Also have a look at those QAs:
serve react frontend and php backend on same domain with nginx
Nginx -- static file serving confusion with root & alias
Deploy both django and react on cloud using nginx
from ngix documentation here, it seems you are missing a / at the end of your paths. this trailing / can cause a lot of pain in many languages to be the root cause of many errors.
please give it a try like this:
# for serving static
location /static/ {
alias /home/user/folder/backend/staticfiles/;
}

Nginx location not resolved

I am trying to have my nginx instance host from two separate static files.
The config for root path works but using any other prefix pattern gives 404 not found.
The config is as follows:
server {
listen 80;
server_name *.saurabhharwande.com;
root /var/www/certbot;
location /abc/ {
index vindex.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
www.saurabhharwande.com Works
www.saurabhharwande.com/abc Gives 404
Am I missing something?
Edit: My basic understanding of how nginx configuration works was flawed. I thought that index file is searched in the root itself /<domain>/<root>/index.html and location is just used to point to different roots. It is rather searched at <domain>/<root>/<location>/index.html

NGINX unexpected behaviour with location directive and proxy_pass

I have a NGINX configuration file to serve a Website with static files and via a development Server.
static -> http://localhost:8080
dev webserver -> http://localhost:8080/dev
There are several other services which I bind to different location directives.
Here is a snipped of the configuration file.
...
upstream qgis {
server qgis-spcluster_server:80;
}
...
server {
listen 8080;
server_name localhost;
location / {
root /usr/share/nginx/html/build;
index index.html index.htm;
auth_basic "Zugangskontrolle";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /dev/ {
proxy_pass http://web_app/;
auth_basic "Zugangskontrolle";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /static/ {
proxy_pass http://web_app/static/;
}
location /qgis/ {
proxy_pass http://qgis/;
}
location /apex/ {
proxy_pass http://apex/apex/;
auth_basic "off";
}
...
Everything works as expected until i open the URL to get the static files. After that all other URLs leads to the static files.
http://localhost:8080/apex -> Apex Service
http://localhost:8080 -> static Website
http://localhost:8080/apex -> static Website
For me everything looks ok, but indeed something is not ok.
The Basic_Auth produce another unexpected behaviour.
http://localhost:8080 -> basic auth -> success -> static website
http://localhost:8080/apex -> basic auth -> it is not possible to get rid of the pop up
So in the moment I'm a little bit clueless how to solve this issue.
Please remove the trailing / from your location directives or provide / when you access them.
Nginx looks for the longest prefix match location. When you access http://localhost:8080/apex, it's routed to / because /apex/ is not the prefix of /apex
Documentation of location is here
I tried now several things, but nothing really worked well. So I decided to create a second server block for all location directives which are problematic with my current setup.
Probably this is not the best solution, because I have still no idea why I get these problems. But it works now and that counts for me.

Nginx location block for specific path and certain file types

I am having trouble defining a location block for certain paths and file types.
I am using wordpress and using a plugin which generates dynamic sitemaps..It redirects to path like sitemapindex.xml, which do not actually exist and nginx is trying to serve it statically.
I need to be able to pass this to apache
I need to send anything that is http://example.com/blog/*.xml to apache. This is what i am trying, which does not work.. so for instance:
http://example.com/blog/post.xml or http://example.com/blog/sitemapindex.xml
nginx config
server {
location ~* ^/blog/*.xml$ {
include /etc/nginx/proxy_params;
proxy_pass http://127.0.0.1:8080;
}
}
what is the correct syntax
Thanks
I had similar problem with my images. In my applications, images were being served from two different locations.
You can specify different sources based on url pattern. Your solution would then look something like this.
location ~* ^/blog/.+\.(xml)$ {
root /some/path/;
expires 90d;
}
location ~* \.(xml|js|jpg|png|css|html|otf|eot|svg|ttf)$ {
root /some/other/path/;
expires 30d;
index index.html;
}
Gotta escape that period
server {
location ~* ^/blog/.*\.xml$ {
proxy_pass http://127.0.0.1:8080;
}
}

Resources