what's wrong with my nginx reverse proxy configuration? - node.js

location/merchant/mangooda
rewrite /merchant/mangooda / break;
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://localhost:9001;
}
This is my nginx configuration. I want to get the localhost:9001 when I go to the server_name/merchant/mangooda. But now when I go to the 'server_name/merchat/mangooda', it's going to the localhost:9001/merchant/mangooda. This path does not exist in localhost:9001. So, I got 404 error. Can anyone fix my problem?
In case 'proxy_pass http://localhost:9001/', when I change the proxy_pass like this, url is working but it doesn't load CSS contents in my NodeJS app. only showing HTML pages.

Related

Express and Nginx figthing over routing

So i have a strange configuration that i need until i get to refactor a project later this year.
it is a static index.html landing page served with a location root "/" by NGINX.
i have a slash location like so:
location /channel/ {
proxy_pass http://node_server:5000/;
proxy_redirect off;
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-Ssl on;
proxy_cache_bypass $http_upgrade;
}
that express.js then handles. Express just serves a static folder with auto-generated content that a user can then navigate around in.
It works well in the sense that any appending slashes like:
/channel/open/
but ONLY if i have a trailing slash after "open". If i leave it out then NGINX will throw a "404 Not Found", if i keep it in, it works as intended.
The thing is that a user should be able to click on a folder in the static served directory to traverse a file structure, and the way that works in a browser is that it does not append a trailing slash to a directory name when clicked, so NGINX then throws the "404".I have tried a NPM package called 'express-slash' but that did not solve the issue.
i was hoping someone here had some suggestions on what i could try out next?
Details about trailing slash behavior can be found here: https://serverfault.com/questions/607615/using-trailing-slashes-in-nginx-configuration
But if your goal is to configure NGINX to allow directory traversal, you may be looking for the autoindex directive: http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

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;
}

rewrite nginx subdirectories to local ports

Hi i´m trying to host several sites on one server using nginx. Therefore i want to do the following:
calling www.domainname.com/site1
shall forward to http://localhost:9800
calling www.domainname.com/site2
shall forward to http:/localhost:9801
the local sites are hosted by nodejs with express, so rewriting the paths are necessary.
What i´ve done so far is the this:
location /site1 {
rewrite ^/site1/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:9800;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
but this only works partly: The stylesheets etc. will not loaded and trying to call
www.domainname.com/site/login
is not working.
Can someone help me writing the correct rewrite ?
Thanks in advance

Nginx second proxy_pass not working

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
}
}

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