My current nginx config look like this
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name api.myapp.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;
}
}
Which host my production webapp at api.example.com but now I need another config for my staging build. I can spin another process of node, make the staging webapp accessible at http://localhost:3002 but since staging webapp also have to be https so should the second block of config be in nginx?
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
listen [::]:443 ssl;
server_name api.myapp.com;
###
ssl configure
###
location / {
if ($server_port = 443) {
proxy_pass http://localhost:3002;
}
if ($server_port = 80) {
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;
}
}
Related
I saw a lot of people ask this question but never answerd. I have a node applocation and Im deploying it using nginx. This occured when I try to deploy multiple sites on same instance. But now I have removed one. This is my config:
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 / {
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;
}
}
the nginx error log:
I just freed the port 8080 and tried installing nginx and pm2 again and it worked.
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
I have a node.js server running on port 5000 behind nginx and after reading several posts, I'm still getting a 502 from nginx. When accessing mydomain.com:5000 in a browser, everything works just fine. Can someone spot what I may be doing wrong here?
upstream backend {
server localhost:5000 max_fails=1 fail_timeout=3s;
keepalive 8;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.com;
ssl_certificate ssl/chained.crt;
ssl_certificate_key ssl/server.key;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection '';
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
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;
proxy_cache off;
proxy_cache_bypass $http_upgrade;
}
}
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;
}
}
I had a Ubuntu Node.js server working with my http://www.example.com website.
I used httpx://localhost:3000 to do my testing, then when I deployed it to Ubuntu,
I still had to enter the port (www.example.com:3000). I was told to implement a
reverse proxy to remove the port 3000 requirement. I installed nginx and added the
following:
sudo nano /etc/nginx/sites-available/default
----------Delete all then Copy / Paste--------------------------
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://67.205.128.21: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;
}
}
This worked, and removed the requirement to enter port 3000.
Then I found out I needed to run my app with a SSL/Certificate.
I was able to make the nginx changes to get it working as https://www.example.com:3000.
But now I need to get rid of the port 3000 requirement.
I tried the same reverse proxy setting that I used for http:, but that did not work.
How do I configure nginx to remove the port 3000 requirement.
Below is what is currently happening when I enter it in my browser:
http://67.205.128.21 - Works
http://example.com - Redirects to https://example ; Error: Redirects too many times
http://www.example.com - Redirects to https://example ; Error: Redirects too many times
http://example.com:3000 - Works
http://www.example.com:3000 - Works
Current nginx configureation:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
location ~ /.well-known {
allow all;
}
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
location / {
proxy_pass http://67.205.128.21: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;
}
}
This should work:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
location ~ /.well-known {
allow all;
}
}
Then, either in the same file or a different file, add an additional server block.
# SSL configuration
#
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
location / {
proxy_pass http://67.205.128.21: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 think the problem was that you only had one server block, and so when the redirect is executed, it falls in that same server block and then redirects again.