I have followed along a digital ocean tutorial to deploy my node.js app onto VPS. Everything is working, but instead of reaching the app from myDomain.com, it's only available through myDomain.com:3700. myDomain.com only shows "Success! Virtual host is set up!"
/etc/nginx.sites-available/default:
server {
listen 3700;
server_name myDomain.com;
location / {
proxy_pass http://127.0.0.1;
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;
}
}
Oddly, if I change it to:
server {
listen 80;
server_name myDomain.com;
location / {
proxy_pass http://127.0.0.1:3700;
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;
}
}
and enter sudo nginx -s reload, nothing changes.
in my node app, I have:
...
var port = 3700;
...
Related
Firstly I explain my issue here.
I have 3 react apps running on 4040 4141 4242 respectively.
Initially, I created 3 servers with three ports like
server {
listen 3000;
location / {
proxy_pass http://localhost:4040;
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;
}
}
server {
listen 3001;
location / {
proxy_pass http://localhost:4141;
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;
}
}
server {
listen 3002;
location / {
proxy_pass http://localhost:4242;
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;
}
}
This was working fine. But I need to serve all these three apps from one port.
eg: http://localhost:8080/ab should redirect to port 4040 and
http://localhost:8080/ac should redirect to port 4141 like so.
Now I tried this way
server {
listen 8080;
server_name localhost;
location /examcontrol {
proxy_pass http://localhost:4040;
}
location /student {
proxy_pass http://localhost:4141;
}
location /staff {
proxy_pass http://localhost:4242;
}
}
This will redirect to the port 4040,4141,4242
but only some tags are visible like <title>,<script>,<noscript> etc no other contents is shown in the browser. (if I change /examcontrol to / I get the correct response and redirected to 4040, but no idea why these paths not working :( )
Please help if anyone knows this to configure it correctly.
I'm pretty sure you could fix this easily by changing your proxy_pass directive (note the trailing /):
proxy_pass http://localhost:4242/;
I am installing a site under my machine. I have a NodeJS server that listens on port 4000.
I am using an NGINX reverse proxy, so that it is accessible from port 80. Here is the following configuration in / sites-availabes
upstream site {
server 127.0.0.1:4000;
}
server {
listen 80;
server_name site.infra.monsite.blog;
location / {
proxy_pass http://site;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
But I can't access my site from site.infra.monsite.blog; which points to port 80 of the machine.
Are there any ports to open like on Windows, or an NGINX configuration that I missed?
Yet when I do "curl -X GET http://localhost/": It works on the machine.
Thanks for your help.
Try this:
upstream site {
server 127.0.0.1:4000;
keepalive 64;
}
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
location / {
proxy_pass http://site;
proxy_http_version 1.1;
proxy_redirect off;
proxy_pass_header Set-Cookie;
proxy_set_header Connection 'upgrade';
proxy_set_header Upgrade $http_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_pass_header X-XSRF-TOKEN;
proxy_read_timeout 240s;
}
I have a test server and try to serve apidoc on node.js with a reverse proxy but in the browser I see a empty page.
apidoc serves with http-server on port 3034 in node.js and I run it with pm2 and this is my default configuration in site-available
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location /docs {
proxy_pass http://localhost:3034;
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 / {
proxy_pass http://localhost: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;
}
}
I'm trying to set up a reverse proxy using nginx for a nodejs application. My node application currently runs on port 8005 of the example.com server. Running the application and going to example.com:8005 the application works perfect. But When I tried to set up nginx my application seems to work at first by going to example.com/test/ but when I try and post or get requests the request wants to use the example.com:8005 url and I end up with a cross origin error, CORS. I would like to have the request url reflect the nginx url but I'm having no luck getting there. Below is my nginx default.conf file.
server {
listen 80;
server_name example;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /test/ {
proxy_pass http://localhost:8005/;
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;
}
}
There got to be some way to tell nginx about whichever app you are using.
So for that, either you can prefix all the apis with say test(location /test/api_uri), and then catch all the urls with prefix /test and proxy_pass them to node, or if there is some specific pattern in your urk, you can catch that pattern with regex, like suppose, all the app1 apis contain app1 somewhere in it, then catch those urls using location ~ /.*app1.* {} location ~ /.*app2.*, make sure that you maintain the order of location.
Demo Code :
server {
...
location /test {
proxy_pass http://localhost:8005/; #app1
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 /test2 {
proxy_pass http://localhost:8006/; #app2
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;
}
...
}
Other Demo for regex,
server {
...
location ~ /.*app1.* {
proxy_pass http://localhost:8005/; #app1
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 ~ /.*app2.* {
proxy_pass http://localhost:8006/; #app2
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;
}
...
}
I still struggling with configuration of nginx for multiple websites in one virtual machine.
So If I do:
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://localhost:8181;
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;
}
}
after hit example1.com my nodejs page is loaded properly. But If I try to add second server block:
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8080;
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;
}
}
Nothing is working, also example1. So I wanna to load through example2.com default nginx location... Something like that:
server {
listen 80;
server_name example1.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8181;
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;
}
}
But it is always redirected to nginx root location.
How can I do that? Thanks for any help!
Node.JS?
You can refer my configuration:
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-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:2368;
}
or another one
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:2387;
}
The first configuration direct to https redirect cycle.
And you said the default site, you can config something like that:
listen 80 default;
server_name xxxxx.com;