I hope this question is not too simple to include here. Any advice would be appreciated.
I have a situation where I am using a single url to host multiple different angular projects on nginx. I use path extensions to differentiate between the sites. It looks something like this:
server {
listen 443 http2 ssl;
#listen [::]:80;
server_name site.local;
. . .
root /usr/share/nginx/html/. . .;
. . .
location /site1 {
alias /usr/share/nginx/html/. . .;
. . .
location /site2 {
alias /usr/share/nginx/html/ . . .;
. . .
What complicates this is that site1 and site2 share an upstream api on tomcat. When these api calls are made, however, "site1" or "site2" gets inserted into the call.
I'm quite ignorant as to how proxying works on nginx, so attempting to use a proxy_path in the location blocks in the simplest way results in calls to nginx (for html files etc.) are also proxied, making the site inaccessible.
Is there a way to set it up such that only upstream calls get rewrote/proxied?
Any advice would be greatly appreciated.
I'm going to make a few assumptions about your build and correct me if I am wrong. but I see two options to resolve this considering they share the same API you can make the changes in the angular build Or in NGINX. Assuming your nginx server name is example.com it contains a location for /site1 and /site2
example.com/site1
example.com/site2
Options 1 Angular
if the URL is public you can simply route calls by setting the environment.ts inside the environments folder.
export const environment = {
production: false,
apiUrl: 'https://example.com/api',
}
You may need to deploy a production environment.ts and a test environment.ts with their respective apiUrl's for each build. But assuming they share the same API all you would need is a nginx location to said upstream API
location /api/ {
proxy_pass upstream_api;
}
Option 2 NGINX
Let's assume you don't want the full API URL in the environment.ts what is the Nginx option. I don't like this method because it's not as D.R.Y and you find yourself writing two nested locations.
location /site1 {
alias /usr/share/nginx/html/. . .;
#any other settings...
#SUB LOCATION TO API
location /site1/api/ {
rewrite /site1/(.*) /$1 break;
proxy_pass upstream_api;
#any other settings...
}
}
location /site2 {
alias /usr/share/nginx/html/. . .;
#any other settings...
#SUB LOCATION TO API
location /site2/api/ {
rewrite /site2/(.*) /$1 break;
proxy_pass upstream_api;
#any other settings...
}
}
Let's break this down.
the first location /site1 will qualify and url using site1 thus your SPA application can route to any url. But when making a request to an API it will seek the 2nd qualifier in the nested location /site1/api/ we use /site1/api so that Nginx knows that /api is not outside of the location.
Lastly rewrite /site1/(.*) /$1 break; will remove the site1 and send the rest of the url upstream to resolve your problem.
Hope this helps.
Related
I am currently running two webservers on the same machine, one using Django through Gunicorn, which is my original site, the other which acts as the online shop using the same domain, this one using Nestjs (Nodejs).
Both servers have an /admin path with the original being at example.com/admin and the other I am wanting to be at example.com/store/admin. However whenever I enter the second URL into my browser (i.e example.com/store/admin) it returns the other admin page, example.com/admin (without the /store prefix).
Here is the config snippet I believe needs reworking:
server {
server_name example.com www.example.com;
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /store {
proxy_pass http://127.0.0.1:3000/;
}
location = /store/admin {
proxy_pass http://127.0.0.1:3000/admin/;
}
...
}
I have tried a fair few combinations of the /store and /store/admin location blocks but just can't seem to get it to direct me to the store's server admin site. It works on my local development machine when testing using the nodejs server. Going to http://example.com/store returns what I expect to see from the Nestjs server.
The docs states that:
... To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. ...
So you need only two location blocks:
The first :
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
And the other with the rewrite:
location /store {
rewrite ^/store(.*) $1 break;
proxy_pass http://127.0.0.1:3000;
}
This means it will rewrite every URL starts with /store and remove it from the URL before passing to the upstream. and this also includes the /admin, since it is the same.
Also note there is no suffix / at the end of the proxy_pass - which instruct NGINX to take the user supplied URI.
I'm still a relative beginner in nginx and hope to get some help and clarification on something I'm working on.
So say I have 2 Node apps, app1 and app2.
I have a production server, but I'm trying to test this locally first.
Currently app1 listens on port 8000 and app2 listens on port 8001.
So currently, they are sitting at localhost:8000 and localhost:8001, and would accessed in the production server as production.example.com:8000 and production.server.com:8001.
My question is, how can I hide the port numbers and assign them to a specific URL?
I want the result to be accessible from localhost/app1 and localhost/app2, and production.example.com/app1 and production.server.com/app2 in the production server.
I don't know what I'm getting wrong in the nginx.conf, so I hope someone can help me on this issue. These apps all have HTML forms, so I need them to post to production.example.com/app1 or something like production.example.com/app2/download. Their CSS breaks as well due to the location of the public folder in each app, since they are only in /public/css.css, not in app2/public/css.css.
I can change all of the form actions and router gets/posts, as well as the stylesheet references by adding /app1 and /app2 respectively in the Node apps, but that feels like I'm doing something wrong, as I shouldn't change any of my router info.
Here is my nginx.conf file:
Edit: So this is what I have right now:
server {
# ...
location /app1 {
rewrite ^/app1$ / break;
rewrite ^/app1/(.*) /$1 break;
proxy_pass http://127.0.0.1:8000;
}
location /app2 {
rewrite ^/app2$ / break;
rewrite ^/app2/(.*) /$1 break;
proxy_pass http://127.0.0.1:8001;
}
}
And I still get the same issue where the node apps themselves are not using their contexts.
So, I have this same configuration with like 4-5 microservices. Here is the configuration that I use.
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /ifttt {
rewrite ^/ifttt$ / break;
rewrite ^/ifttt/(.*) /$1 break;
proxy_pass http://127.0.0.1:8080;
}
location /actions {
rewrite ^/actions$ / break;
rewrite ^/actions/(.*) /$1 break;
proxy_pass http://127.0.0.1:5000;
}
location /events {
rewrite ^/events$ / break;
rewrite ^/events/(.*) /$1 break;
proxy_pass http://127.0.0.1:5050;
}
location / {
proxy_pass http://127.0.0.1:8081;
}
}
Hope it serves as a constructive reference. Let's take one,
location /actions {
rewrite ^/actions$ / break;
rewrite ^/actions/(.*) /$1 break;
proxy_pass http://127.0.0.1:5000;
}
So, the first rewrite matches only localhost/actions and forwards it to http://127.0.0.1:5000
The second one matches localhost/actions/<anything> and forwards it to http://127.0.0.1:5000/<anything>
I think you're missing the slash (/) before the regex match.
Edit:
Using Your comment as reference
The index is at /app1/index
a page is at /app1/index/flashfireblast
and the navbar header needs to reference /app1/stylesheets/css.css
So, To address /app1/stylesheets/css.css from /app1/index/flashfireblast you need to add ../stylesheets/css.css as the stylesheet href.
For reference:
If current directory is /var/www
1) /: means Root. /
2) ./: means Current Directory. /var/www
3) ../: means Previous directory. /var/
I'm running a node.js API on a VPS, which is served by nginx on Ubuntu 13.04.
I'm using restify and serving static files like this:
server.get(
/\/static\/?.*/,
restify.serveStatic({
directory: __dirname // => /home/misha/rxviz-api
})
);
Here is the relevant bits from nginx config:
server {
listen 80;
server_name api.rxviz.com;
location / {
proxy_pass http://localhost:4010/;
}
}
(full config is here)
When running:
curl http://localhost:4010/static/.well-known/acme-challenge/test.json
on the VPS, I get the contents of test.json.
However, when navigating to:
http://api.rxviz.com/static/.well-known/acme-challenge/test.json
in a browser, I get 404.
nginx error logs show that /opt/nginx/html/static/.well-known/acme-challenge/test.json not found.
Why does nginx trying to access test.json in /opt/nginx/html rather than /home/misha/rxviz-api?
Few more points:
static directory permissions are drwxrwxr-x
I can access http://api.rxviz.com/ in the browser successfully
It's hard for me to be sure, as I can't test it, BUT..
I think you have a problem with the nginx settings:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
the /static is appended to the alias including the location part, so it's searches for /home/misha/rxviz-api/static/static
This is going to return 404 since there is no static/ within static/
try to fix to:
location /static/ {
alias /home/misha/rxviz-api/;
}
....
...
and also I wanted just to mention.
It's seems like your node server (using restify) is serving the content as you wanted (127.0.0.1:4010/static/... works as you say),
BUT note that nginx is anyway not redirecting any of the /static calls into your node, so your node restify is unused.
why?
This section:
location /static/ {
alias /home/misha/rxviz-api/static/;
}
tells nginx to NOT redirect those /static calls into your node service, and instead try to find and serve the file from the local path.
so if you want to use the restify... just remove this entire location /static part, and your node will serve those files too.
Consider the following nginx config file:
server {
listen 443;
ssl on;
ssl_certificate /etc/tls/cert.pem;
ssl_certificate_key /etc/tls/key.pem;
location / {
proxy_pass http://api.default.svc.cluster.local;
}
}
All incoming TCP requests on 443 should redirect to my server running on api.default.svc.cluster.local:80 (which is a node REST-Api btw). This works fine, I can curl https://<nginx-IP>/ nginx and get a correct response, as expected.
Now, I'd like to change the location from / to /api, so I can fire a curl https://<nginx-IP>/api in order to get the same response as before.
1. Attempt
So I change the location line in the config to:
location /api {
Unfortunately this won't work, instead I get an error Cannot GET /api which is a node error, so obviously it gets routed to the api but something's still smelly.
2. Attempt
It seems as the trailing slash in an URI is required so I added it to the location:
location /api/ {
Now something changed. I won't get the same error as before, instead I get an "301 moved permanently". How can I fix my nginx config file?
Additional information regarding the environment
I'm using a kubernetes deployment that deploys the nginx reverse proxy incl. the config introduced. I then expose nginx using a kubernetes service. Also, I tried using kubernetes ingress to deal with this situation, using the same routes, however, the ingress service would respond with a default backend - 404 message.
As mentioned in the question, trailing slashes in URIs are important. I fixed this in the location, however, I didn't add it to the URI I pass using proxy_pass.
As for the nginx proxy I got it to work using the following config:
server {
listen 443;
ssl on;
ssl_certificate /etc/tls/cert.pem;
ssl_certificate_key /etc/tls/key.pem;
location /api/ {
proxy_pass http://api.default.svc.cluster.local/;
}
}
Concerning the ingress solution, I was not able to get it to work by adding the missing trailing slash to the path. The service is specified due its name and therefore no trailing slash can be added (i.e. it would result in an error).
I am completely stuck with a situation where I want to have several node applications on one server. I get this working fine by having the applications running on different ports. I can access the applications by putting in the ip address with port.
I would like to proxy the applications from my nginx server by using different sub-directories like so:
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001;
}
location /app2 {
proxy_pass http://10.131.6.181:3002;
}
Doing this I had to move the all the express routes to /app1 for application1. This works but now I am stuck with the static files.
I can now access the application with http://10.131.6.181:3001/app1 which is great, but via http://my.domain/app1 the static files are not loaded.
The static files can be accessed directly http://10.131.6.181:3001/css but not via the proxy http://my.domain/css
Ideally I would like to have the applications on different ports without the sub-directory in the express routes but only sub-directories in the proxy. I tried to put my head through the wall for the last 5 hours but didn't achieve anything.
Now I would happy if can at least get the static files via the nginx proxy.
An updated answer for anyone who needs:
instead of
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
use
location /app1/ {
proxy_pass http://10.131.6.181:3001/;
}
or if on local
location /app1/ {
proxy_pass http://localhost:3000/;
}
This is the correct way and this way you will not need to modify express. Express will receive only the part after /app1/
I finally worked it out after a google surge.
I added the directories to the nginx proxy_pass
my.domain
location /app1 {
proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
proxy_pass http://10.131.6.181:3002/app2;
}
And I had to change the express applications to use the subdirectory
app.use('/app1', express.static(path.join(__dirname, 'public')));
app.use('/app1'', require('./routes'));
In the router I had to prefix all the redirects.
router.get('/logout', function (req, res) {
req.logout();
res.redirect('/app1/login');
});
The static files are called like so from html
<link rel="stylesheet" href="/app1/css/style.css"/>
A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...