Nginx second proxy_pass not working - node.js

I have nginx, apache, and node setup on my CentOS server.
Node runs on port 8080.
In my default.conf nginx file, I have
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
That works. When going to my site's homepage (and any other page that node uses, such as /login and /signup page), I see my node app correctly.
I have a couple of other rules like this one, in order to let nginx serve the static files instead of node serving them.
location /javascripts {
root /var/www/myWebsite/public;
}
I am setting up a forum, and I'm using apache and php for it. The forum works at myWebsite.com:90, however I want to make the forum work without the port number.
Apache has Listen 90, and DocumentRoot is set to the forum path.
Nginx conf has this
location /forum {
proxy_pass http://127.0.0.1:90;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
But that doesn't work, I get a 404. If I go to some giberrish page, I get the node's error page, so we can see that something is working by getting the 'normal' web 404 instead of my custom node's 404.
How can I remove that :90 port now?
Thanks

upstream apache_server {
server 127.0.0.1:90;
keepalive 60;
}
server {
listen 80;
location / {
proxy_pass http://apache_server
}
}

Related

How to configure Nginx as reverse proxy to do this network schema?

I have two Docker instances running Wordpress and my Nodejs application separately in the same machine.
Now I want to set up Nginx to accept connections only on my domain (mydomain.com). So if users access to mydomain.com/ they should be redirected to wordpress container, and if they access to mydomain.com/customer, the request should be redirected to Nodejs app container.
This schema explains the idea:
I am not very good at Nginx and it is being so difficult for me to edit the config. file to match that schema. Also I dont find any kind of examples or documentation to achieve this configuration. Is it so weird?
It is the very common nginx usage example. The very basic setup you need is
server {
listen 80;
server_name mydomain.com;
proxy_set-header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://<docker1_address:port>;
}
location /customer {
proxy_pass http://<docker2_address:port>;
}
}
server {
# this would be the default server for all requests
# where HTTP Host header is missing or not equal to 'mydomain.com'
listen 80 default_server;
# close the connection immediately
return 444;
}

nginx link nodejs app to subdomain

I have a dropplet on digital-ocean where I have a node.js app and nginx.
So, I installed nginx and then in /etc/nginx/sites-available/ I created a new file called api where I have the following content:
server {
server_name api.my-website.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 $proxy_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:9000; // this is where the node app lives
}
}
After I create that I restared nginx but if I go to api.my-website.com I dont see anything (my api has a landing page on /), but if I go to my-website.com:9000 I see the landing that should be on api.my-website.com.
Can someone explain me please what I'm doing wrong?
You did not included your /etc/nginx/sites-available/api file into /etc/nginx/nginx.conf. You need to include /etc/nginx/sites-available/api in /etc/nginx/nginx.conf inside http module and then restart or reload nginx. Your /etc/nginx/nginx.conf may looks like
http {
# other lines for logging, gzip, etc.
# other include may here
# Your include as following
include /etc/nginx/sites-available/api;
}

Serve static site and node app on port 80 with nginx?

So now I am serving my backend app on mysite:4300 and my static site on mysite:80. This is okay but causes a few problems, one being SSL I would have to get two signed certificates, at least I think.
Another problem which is CORS, it's not a big issue my express app is configured to allow CORS, but I would like to serve it all under one origin.
Here is how my nginx config looks.
I created inside /etc/nginx/conf.d/mysite.com.conf
server {
listen 80;
server_name mysite.com;
location / {
proxy_pass http://localhost:3100; //node js port
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;
}
}
So basically the above allows me to serve my nodejs app (running with forever.js) for port :3100 on port :80.
This hijacks my static site, obviously but I was wondering how I could possibly configure it to serve on mysite.com/myApp
My Question
How do I configure nginx to serve as a proxy not to mysite.com:80 but mysite.com:80/myApp so I can serve my static website on mysite.com:80?
Do I need to rethink how I am using the proxy, or is there a configuration method I can use?
P.S Following this tut https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab
Maybe I need to configure DNS records, or create a subdomain?
Solution: I ended up using a subdomain, but I think it's pretty much the same concpet.
Thanks to #Peter Lyons I have this server block
server {
listen 80;
server_name app.mysite.com;
location / {
root /var/www/mySite/public_html;
proxy_pass http://localhost:3100;
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 #app {
proxy_pass http://localhost:3100;
}
}
It wasn't working until I added a root and location #app so now it works fine on port:80 so no more having a port number exposed in the url. Hopefully I can setup SSL for this now!
I think I am going to try and serve it like this mysite.com/myApp to test it, might be handy in the future.
P.S I may also avoid using the subdomain, because it still is considered Cross origin Are AJAX calls to a sub-domain considered Cross Site Scripting?
I may want to allow my app to communicate with my site, and avoiding CORS might make it easier. That is if mysite.com/myAPP is not considered CORS either. We will see.
Try: proxy_pass http://localhost:3100/myApp$uri;, which I think should do what you want.
When I want nginx to serve static files, I use try_files like this:
location / {
root /path/to/my/app/wwwroot;
try_files $uri $uri.html $uri/index.html #app;
}
location #app {
proxy_pass http://localhost:3100;
}

NGINX dns is not working on VPS

I'm a little new to VPS/Linux so please bear with me.
I have a domain name (attendahh.com) pointed to my host's nameservers.
I've set up /etc/nginx/conf.d/attendahh.com.conf as follows:
# the IP(s) on which your node server is running. I chose port 3000.
upstream attendahh.com {
server 127.0.0.1:1999;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name attendahh.com attendahh;
access_log /var/log/nginx/attendahh.log;
# pass the request to the node.js server with the correct headers and much $
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://attendahh.com/;
proxy_redirect off;
}
}
Then I service nginx restart.
I've read a bunch of tutorials and stack answers this is ostensibly all I need to do, but if I go to http://attendahh.com it does not work.
Things to note:
Going to my IP + port in the browser works just fine ( 23.226.227.16:1999 )
I have Cpanel for VPS installed (I tried to set up the DNS in there originally but it didn't work, I've since deleted the dns entry from there but it may still be affecting things)
Apache virtual hosts are commented out in httpd.config.
Any ideas on what I'm doing wrong here? Perhaps there's some conflict between apache and nginx?
- proxy_pass http://attendahh.com/;
+ proxy_pass http://attendahh.com;
Nginx uses its own resolver, it doesn't use the system's resolver (/etc/resolver).
You have to configure it using the resolver directive. If you are not using the resolver directive, then use IP address in the proxy_pass directive.
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

Subdomain problems using nginx as a reverse proxy for node apps

I am new to nginx and I'm struggling to get my configuration for a reverse proxy working. I have a node app running on localhost:3010 and I'm trying to serve pages through nginx from this app at the subdomain dev.[sitename].org. Let's just say dev.example.org for readability. Here are the contents of a file I created in sites-available called example.org (is that the correct name for this file?):
server {
server_name www.example.org example.org;
}
upstream app_dev.example.org {
server 127.0.0.1:3010;
}
server {
listen 0.0.0.0:80;
server_name dev.example.org;
access_log /var/log/nginx/dev.example.access.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_dev.example.org/;
proxy_redirect off;
}
}
This is mostly based off this related question: Node.js + Nginx - What now? however when I try to open dev.example.org in my browser, Chrome reports that it can't find the page. I can ping dev.example.org and get an IP address, so the server seems to be available, but my nginx configuration incorrect. I created the symlink in sites-enabled and restarted nginx, in case you thought I might have forgotten those steps.
So my thought now is that I'm not referring to the subdomain correctly somewhere, or maybe my file in sites-available is named wrong. Any push in the right direction would be appreciated.
Just to be sure the problem is on nginx try these steps:
Set a test server at port 3030, serving the system doc folder or anything else.
server {
listen 3030
location / {
root /usr/share/doc/;
autoindex on;
}
}
upstream simple_test {
server 127.0.0.1:3030
}
Then use simple_test below as well:
proxy_pass http://simple_test/;
If you see the /usr/share/doc dir listing when you access dev.example.org then your issue is on the node side.
Turned out something was blocking port 80! fixed that and the config as posted above worked

Resources