Nginx Configuration Issue with sub-domains on NodeJS - node.js

I have three web sites working with NodeJS. I want to publish with NginX, therefore have installed all its requirements. For my domain, I'd like to publish over https, whereas for sub-domains, I'd like to publish over http. My problem is that the publishing fails for sub-domains.
I have written the config files in /sites-enabledfolder.
/default:
server {
listen 443 ssl;
server_name www.my-domain.com;
ssl_certificate /var/www/my-domain/server/config/certificates/www_my-domain_com.crt;
ssl_certificate_key /var/www/my-domain/server/config/certificates/www_my-domain_com_nokey.key;
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;
}
}
server {
listen 80;
server_name www.my-domain.com my-domain.com;
return 301 https://www.my-domain.com$request_uri;
}
server {
listen 443;
server_name my-domain.com;
return 301 https://www.my-domain.com$request_uri;
}
/subdomain.my-domain.com:
server {
listen 80;
server_name crm.my-domain.com;
access_log /var/log/nginx/crm.my-domain.com.log;
location / {
proxy_pass http://localhost:8081;
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;
}
}
/subdomain2.my-domain.com:
server {
listen 80;
server_name support.my-domain.com;
access_log /var/log/nginx/support.my-domain.com.log;
location / {
proxy_pass http://localhost:8082;
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;
}
}

Related

Why my reverse proxy (nginx) does not work

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

ngix 2 server block in one linux server

I can't restart nginx because I got [emerg] 6594#6594: bind() to 0.0.0.0:443 failed (98: Address already in use). How does multiple server block work? Without the staging server block my config is working fine.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name api.example.com;
location / {
proxy_pass http://localhost:3001;
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 443;
server_name staging-api.example.com;
location / {
proxy_pass http://localhost:3002;
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;
}
}
netstat -anp | grep :443 chgeck which app take over the port 443,and if not necessary, kill it and then restart nginx

can't use apidoc with reverse proxy in nginx

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

Port number needed after domain name

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

Multiple websites in one VPS instance with NGINX

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;

Resources