I am planning to run 2 node apps with express under one sub domain, with nginx serving the static files. In both node apps I use:
app.use(express.static(path.join(__dirname)));
I have the following nginx config:
server {
listen 80;
server_name sub.domain.com;
index index.html;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://localhost:232;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
}
With this setup everything is great for the 'main' app (accessed at sub.domain.com), but for the 'admin' app (sub.domain.com/admin) the same static files are served as for the 'main' app. How should I modify my setup to achieve the correct behavior?
Check using your dev-tools to make sure that the static assets are attempting to be loaded from the correct URL; that is, http://sub.domain.com/admin/css/ or /admin/css/ etc. It is possible that the paths are being generated with respect to their projects root and appear as /css/, so when the browser tries to retrieve them, it goes to http://sub.domain.com/css/.
I think the order of location blocks in your configuration may be the issue. For more detailed info, check out this link. Try using this refactored configuration -
server {
listen 80;
server_name sub.domain.com;
index index.html;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location / {
proxy_pass http://localhost:232;
}
}
Related
I have 2 NodeJS applications running locally on port 3000 and 3001. I'm trying to route to them individually by using my_ip/tilbudsfinneren for one of them, and my_ip/hms for the other one.
My sites-available file looks like this:
server {
listen 80;
listen [::]:80;
root /var/www/my_ip/html;
index index.html index.htm index.nginx-debian.html;
server_name my_ip www.my_ip;
location /tilbudsfinneren {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /hms {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Whenever I try to reach my_ip/hms or my_ip/hms/ping it does not reach the NodeJS application, I get get "Cannot GET /hms". Is there an issue with my nginx configuration, or is there maybe something else wrong?
If you want to serve both apps as reverse proxy you must use rewrite methods in both locations.
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
location /tilbudsfinneren/ {
rewrite ^/tilbudsfinneren/(.*)$ /$1 break;
proxy_pass http://localhost:3000;
#other config...
}
location /hms/ {
rewrite ^/hms/(.*)$ /$1 break;
proxy_pass http://localhost:3001;
}
}
/etc/nginx/nginx.conf file
in http section, given the below informaiton.
include /etc/nginx/conf.d/*.conf;
/etc/nginx/conf.d/myapp.conf file content
server {
listen 80;
listen [::]:80;
server_name _;
index index.html index.htm;
location / {
proxy_pass 'http://XXXXX:3005/';
}
location /admin/ {
proxy_pass 'http://XXXX:3000/';
}
}
http://XXXX is redirecting to the main page.
http://xxxxx/admin/ is not redirecting. Instead it is throwing below errors in chrome console.
error image
The same site is working fine http://xxxx:3000 it is working fine. No issues.
Whatelse am I missing?
Note: OS is Amazon Linux & I have tested the same configuration with sample help world node js program & it is working fine.
Make sure that your routing is correct and that you have restarted nginx after configuration.
Try the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /admin {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I have two node.js apps: one is my site and second is CI tool. I have the following nginx config:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8081/;
}
location /ci {
rewrite ^/ci(.*) /$1 break;
proxy_pass http://127.0.0.1:3000/;
}
}
When i open example.com in browser it loads all resources requested by index.html correctly, but when i open example.com/ci it loads only index.html and i got errors like these:
GET http://example.com/styles/styles.css 404
GET http://example.com/scripts/app.js 404
I have added <base href="/ci"> into head of index.html in my CI app, but it doesn't help.
I ended up with next nginx config:
server {
listen 80;
server_name example.com;
location / {
if ($http_referer ~* example.com/ci) {
rewrite (.*) http://example.com/ci$1;
}
access_log off;
proxy_pass http://127.0.0.1:8081/;
proxy_set_header Host $host;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location = /ci { rewrite ^ /ci/ last; }
location /ci/ {
access_log off;
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Host $host;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Also on the client side of my CI app i specified domain and port for socket.io connection: io.connect('http://example.com:3000').
No <base> html tags needed.
I have a Node.js app running on port 3000 and using NGINX as a proxy. Static files are also being served by NGINX. The conf in sites-enabled:
server {
listen 80;
server_name myapp.dev;
location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
root /srv/nodejs/myapp/public;
access_log off;
expires max;
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Everything is working fine at the moment. But when a non-existing static file is requested (/css/doesntexist.css) I get a the default NGINX 404 page.
I know it's possible to redirect the user tot a custom 404 page (eg. /404.html), but I want to keep the URL pointing to the non-existing path while displaying a custom 404 from my Node app.
Any ideas?
Got it working with the following.
location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
access_log off;
expires max;
error_page 404 = #not_found;
}
location #not_found {
proxy_pass http://127.0.0.1:3000;
}
Which is based on an example from the NGINX documentation for the error_page option.
I'm trying to host 2 applications in the same droplet in digitalocean by using nginx
But so far I've only been able to get the root application running (the one without example.com/secondapp)
I want to be able to not use a subdomain and just use example.com/secondmeteorapp to be able to access it.
My sites-enabled/default looks like this:
server {
listen 80;
#listen 443 ssl;
server_name example.com/;
#ssl_certificate /etc/nginx/ssl/ssl-bundle-myApp-domain-com.crt;
#ssl_certificate_key /etc/nginx/ssl/myApp_domain_com.key;
location /dragonfire {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
}
location /images {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location /fonts {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location "^/[a-z0-9]{40}\.(css|js)$" {
root /home/dragonfire-build/bundle/programs/web.browser;
access_log off;
expires max;
}
}
however, when I access http://serverIpAddress/dragonfire it can't find the css or javascript giving me this error:
GET http://myipaddress/1f3848edee9e199050b9b1965b9e697aa714b9f3.css?meteor_css_resource=true
GET http://myipaddress/6e48198c6b584ff79c86e0c624a65b4853faaf50.js?meteor_js_resource=true 404 (Not Found)
I can access the app if I go directly through the IP address and port but not via the nginx way
QUESTION
How can I access a second app using the same domain but with a /mysecondapp (in this case /dragonfire at the end?