I run my node app on localhost:3000 and it is serving a default page for the route /. If I access http://localhost:3000 the default page is displayed accordingly. I have also running a Nginx server that is basically configured as followed:
server {
listen 80;
server_name localhost;
location /node_app {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I run http://localhost/node_app now, my node app throws an error saying that it cannot find route /node_app.
How can I configure either my node app or the nginx server in a way that I can access the app by calling http://localhost/node_app, yet the app itself thinks it is at /?
Update
If I add a / to http://127.0.0.1:3000 it is actually matching /node_app to the / route. But now every stylesheet for instance within the default page is now pointing to the wrong path.
After experimenting a bit around I finally got the configuration right to work exactly how I wanted the server to work:
server {
location /node_app/ {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Lesson learned: Remember the slashes!
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.
For example. User request http://user.dist.com, the request will firstly arrived at nginx server,
upstream a.hello.com {
server 10.243.26.104:8800;
}
server
{
listen 80;
server_name user.dist.com;
location /
{
proxy_pass http://a.hello.com;
}
}
and the server a.hello.com is running at Node.js, the Node sever want to get the real origin request host which should be user.dist.com, but now, the Node server get a.hello.com, so how to get the origin host?
you can use proxy_set_header-
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
i’m new to the javascript world and im trying to publish online a small react app that i‘ve done for tests.
So, first i installed nodejs in my server that already has nginx. Then expressjs, configure it and made a nginx .conf file that uses proxy_reverse to access port 8080 (chosen for nodejs). When i access the server name of that .conf file, without the port, the page it’s shown correctly.
The problem is, if i access another server name configured in my server and add the 8080 port at the end, nginx shows me my nodejs app, even with a server name different from the one configured in proxy_reverse.
Is there any way to limit access to that specific port to one specific server name and return a error everywhere else?
My nginx conf file:
server {
listen 80;
server_name react.mydomain.com;
access_log /var/log/nginx/react.access.log;
error_log /var/log/nginx/react.error.log error;
location / {
proxy_pass https://localhost:8080;
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;
# Enables WS support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
Thanks
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