I've got two IPs associated with my VPS, and am trying to set this up to serve two node apps. Here's my configuration:
In /etc/nginx/sites-enabled/domain1:
upstream app_domain1 {
server 127.0.0.1:4000;
}
server {
listen 0.0.0.0:80;
server_name IP1.xxx.xxx.xxx;
access_log /var/log/nginx/domain1.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nninx-Proxy true;
proxy_pass http://app_domain1/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled/domain2
upstream app_domain2 {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name IP2.xxx.xxx.xxx;
access_log /var/log/nginx/domain2.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nninx-Proxy true;
proxy_pass http://app_domain2/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled, I ran:
ln -s /etc/nginx/sites-available/domain1 domain1
ln -s /etc/nginx/sites-available/domain2 domain2
Now, when I go to /var/www/domain1 and run "node app.js" on the correct port, I can visit the relevant IP address and see the app running, but the same is not true for domain2 (I checked that it's running on the correct port for this config. The request just times out - no response at all.
So how can I troubleshoot this?
Update:
If I go to the ports directly, I see both apps available on both IPs, so:
IP1.xxx.xxx.xxx:4000 gives me the app for domain1
IP1.xxx.xxx.xxx:3000 gives me the app for domain2
and
IP2.xxx.xxx.xxx:4000 gives me the app for domain1
IP2.xxx.xxx.xxx:3000 gives me the app for domain2
So it's treating each IP address as the same.
server_name accepts only domains but not IP addresses.
You have typo mistake in proxy headers, change this:
proxy_set_header X-Nninx-Proxy true;
with this:
proxy_set_header X-NginX-Proxy true;
Related
I made a fork of the freecodecamp project on github and modified the design to meet my client's requirements. Locally, everything works fine, but when I deploy online on a digitalocean droplet with Nginx as a proxy, There is a problem that occurs when authenticating with Auth0, the access-token is not sent to the client. Basically, the freecodecamp application uses auth0 to handle all the authentication.
Since everything is working fine locally, I thought that my online Nginx configurations might be the problem.
I created two configuration files on Nginx, one for the client and one for the api.
The configuration file for the api has the following content:
server {
listen 80;
listen [::]:80;
root /var/www/html/freeCodeCamp;
server_name my_domain_name.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect http://localhost:3000/ http://$server_name/;
}
}
The configuration file for the client has the following content:
server {
listen 80;
listen [::]:80;
root /var/www/html/freeCodeCamp;
server_name my_domain_name.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost:8000/ http://$server_name/;
}
}
I would like to have your opinion on the subject. Thanks in advance.
I am having a problem.
I have a node application running on :3000 on a subdomain inside of Plesk.
I have the subdomain https://xxx.flamingocams.co.uk ;
when I navigate to the subdomain it displays the default plesk page and this is the problem;
I have tried to change the port of the node application to 80 and 443 however this conflicts with plesk.
I have no issues when accessing the node application on https://xxx.flamingocams.co.uk:3000.
Now the only other thing I've seen other people attempt is a reverse proxy;
I found this example;
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
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_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
I am running Plesk Obsidian v18.0.34_build1800210325.10 os_Ubuntu 16.04 so my question is, Where would I place this config to get the subdomain to point only to the nodejs application?
And is this config correct for what I'm trying to achieve?
I have little to no knowledge on nginx configuration my apologies
I have checked out this post and the answer says I need to add a config /etc/nginx/sites-available/yourdomain.com however I do not have the directory sites-available
response to comments // xxx.flamingocams.co.uk.conf
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
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_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate /opt/psa/var/certificates/scfZc0CwJ;
ssl_certificate_key /opt/psa/var/certificates/scfZc0CwJ;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
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_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
In your config, the server is running only on port 80
listen 0.0.0.0:80;
With this config, the page : http://xxx.flamingocams.co.uk will display your application (the request come to the NginX proxy then is forwarded to NodeJS application on port 3000). Because there aren't any server block listen on port 443, the default Plesk screen is displayed.
In order to have your app running on https, you need to listen on port 443 on NginX, you also need to configure the SSL certificate
The config would be :
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate path_to_your_ssl_certificate;
ssl_certificate_key path_to_your_ssl_key;
# The rest of your config is ok :)
}
I currently have a node/angular app that runs as expected when pointed directly to the port configured (8081 for the purposes of explaining my situation). I'm able to post,get,put,delete as expected.
My goal is to have the node application running at mydomain.com/subfolder. When nginx is configured with the location of '/', everything works as expected. Config below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
As soon as I change the location to /subfolder, however, my get,post,put,delete requests return 404 responses. The index.html configured in the node application is returned though. Configuration below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /subfolder {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
In my angular factory, I have my requests structured like return $http.get('/subfolder'); or return $http.post('/subfolder', {data: data});.
And, within my node application, I have the routes defined like app.get('/subfolder', somefunction); or app.post('/subfolder', somefunction);
Again, when I have the application running from the root of the domain, it works fine. When I have it configured to be in a subfolder of the domain, however, the requests no longer work.
My end goal would be to have multiple node applications running from sub-folders of a main domain. I've been fighting with this for a while, and found several articles for hosting mutliple node apps on a single server, but they seem geared toward having separate domains. I'd like (if possible) for these to run as separate apps for the same domain.
Any thoughts/tricks/pointers? Thanks!
Modify Your Nginx File to look like this:
upstream node{
server 127.0.0.1:3000;
}
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /node {
rewrite /node(.*) $1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node;
proxy_redirect http://node/ /node;
}
I got the above from here: http://skovalyov.blogspot.com/2012/07/deploy-multiple-node-applications-on.html and it works for me
At the moment I'm handling CORs within node.js which seems to be working pretty well; I can setup multiple domains and hit my node.js code just fine.
The trouble is once I put node.js inside of forever and run a proxy from nginx to my application it no longer handles any CORs requests. In fact they never even make it to the server and I'm having trouble figuring out why. Below is my nginx configuration; any suggestions? If it isn't obvious I'm trying to hit the services.org from the webapp.org. In this environment they're on the same box but in other environments and when consumed by third parties we need CORs working.
server {
listen 80;
server_name webapp.org;
location / {
proxy_pass http://localhost:3000/Site/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
server {
listen 80;
server_name services.org;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
Try 127.0.0.1 instead of localhost in the nginx.conf
Try adding an upstream server like this:
upstream my_app {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name webapp.org;
access_log /var/log/nginx/my_app.log;
# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
root /var/www/my_app/public;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://my_app;
break;
}
}
}
You may need to enable CORS for the service via:
app.enableCors() in your bootstrap() function in main.ts
I would like to host 2 different node applications with nginx from the same domain and am having some trouble. I would like to have:
mydomain.com point to node app firstApp and otherapp.mydomain.com point to node app otherapp
Right now, I can access firstApp just fine, but I cannot access otherapp via otherapp.mydomain.com.
My config for firstApp looks like this:
upstream firstApp{
server 127.0.0.1:8123;
}
server{
server_name mydomain.com;
access_log /var/log/nginx/me.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://firstApp/;
proxy_redirect off;
}
}
My config for otherapp looks like this:
upstream otherapp{
server 127.0.0.1:8124;
}
server{
server_name otherapp.mydomain.com;
access_log /var/log/nginx/me.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://otherapp/;
proxy_redirect off;
}
}
I have created both configurations in the nginx sites-available directory, they are both linked in the sites-enabled directory, and I have restarted nginx. Can someone tell me what I'm doing wrong?
Thanks,
Swaraj
Just found out what the problem was. Though my nginx configs were correct, I had not added my desired subdomain to my domain name provider (namecheap). I added my subdomain on namecheap, and everything is working correctly now.
you should config your nginx file like this
server {
listen 80;
server_name biger.yourdomain.cn;
access_log /data/log/nginx/access_ab.log;
error_log /data/log/nginx/error_ab.log;
location /firstApp {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8001/;
}
}
maeby you need add this code to your project
app.enable('trust proxy');
I was facing the same problem, after spending time on research I wrote a blogpost where I explained with details how I solved it, I hope it helps. Here it is: http://blog.donaldderek.com/2013/08/cf-i-configure-your-staging-machine-with-node-js-and-nginx/