Handling CORs inside of node behind nginx - node.js

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

Related

Nginx Minio static files 404 on a context path

I have 2 machines: One holds the Minio running in a Docker container on port 9001 and the other holds the Nginx. I want to access\serve Minio on a path prefix /media i.e. www.mydomain.com/media: I can see that proxy_pass is working fine but I'm getting 404 on static files:
I can see that the main page is getting loaded by checking the favicon and the page title.
Below is my nginx config file:
...
upstream minio {
ip_hash;
server <hostname\IP>:9001;
}
server {
server_name mydomain.com;
...
location /media {
rewrite ^/media(/.*)$ $1 break;
proxy_pass http://minio/;
client_max_body_size 0;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
chunked_transfer_encoding off;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_connect_timeout 300;
}
...
}
Any help would be highly appreciated. :)
Unfortunately it seems like this isn't possible and the MinIO team isn't interested in supporting it natively:
https://github.com/minio/minio-js/issues/737
This comment suggests some workarounds, however: https://github.com/minio/minio-js/issues/737#issuecomment-809373153
reverse proxy via nginx - rewrite prefix and added option proxy_set_header Host '127.0.0.1:9000'
use traefik with stripPrefix and sets static header Host

How to keep host when proxy to other servers in nginx

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;

Nginx not listening to ports

I am trying to set up a Nginx as a reverse proxy to access multiple NodeJS apps running on the same server.
I have my nodeJS apps running with PM2 and it all seems fine:
My nodeJS app is the simple nodeJS app generated with express-generator, so it is supposed to be running on port 3000.
I have also set up my Nginx with the following config
server {
listen 1004;
server_name pumadashboard.com;
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;
}
}
However when I curl 127.0.0.1:1004 I get a badGateway error from Nginx. I am also not able to access pumadashboard.com from anywhere on my local network, it just loads until timeout.
What do you get if you do:
curl http://locahost:3000
This should give back a response and like that you will understand if the application started properly.
This nginx configuration works for me
upstream pumadashboard.com {
server 127.0.0.1:3010;
}
server {
listen 80;
server_name pumadashboard.com;
root <path to your node application>;
access_log /var/log/nginx/your-access.access.log;
error_log /var/log/nginx/your-error.error.log;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://pumadashboard.com/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
Try adding
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port 80;
to your location / block

Nginx, Node, Angular - Subfolder API/URL configuration

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

Serving multiple node apps with nginx on same domain

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/

Resources