EC2 Node backend app [504 gateway timeout] - nginx

Works for a couple of hours and then I receive a 504 gateway timeout error on the backend of the application.
EC2 instance is running ubuntu with nginx and PM2.
/etc/nginx/sites-available .conf file:
server {
listen 80;
server_name mydomain.com;
root /home/ubuntu/app;
index index.html;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
location / {
try_files $uri /index.html =404;
}
}
server {
listen 8080;
server_name mydomain.com;
location / {
proxy_pass http://127.0.0.1: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;
proxy_redirect off;
}
}
I believe my configuration is correct as it works correctly, but only for a few hours. Then I get the error. PM2 instance is still online and running. I think it's probably the node app crashing for some reason, but how can I troubleshoot this on ubuntu EC2? It works perfectly on my local machine.
Any suggestions would be appreciated.

Spent hours on this. Turns out PM2 and Nginx don't always work well together, changing the Nginx config and restarting Nginx and pm2 eventually fixed the problem for me.
Specifically, adding these two lines:
proxy_set_header Connection '';
keepalive_timeout 10;

Related

Can't get Pocketbase running behind Ngnix as a reverse proxy

I want to use Pocketbase behind Ngnix as a reverse proxy on my Ubuntu-VPS. I followed the documentation on https://pocketbase.io/docs/going-to-production/.
I wanted to put pocketbase to /api/. When i try to connect to the pocketbase admin panel the browser shows some 404 and a ContentSecurityPolicy Error. It looks like this:
It also seems to be that some HTML is loaded from Pocketbase.
This is my current ngnix config (i replaced my domain with test.com)
server {
listen 80;
listen 443 ssl;
server_name test.com;
ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
location / {
try_files $uri $uri/ /index.html;
root /var/www/html;
index index.html;
}
location /api/ {
# check http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
proxy_set_header Connection '';
proxy_http_version 1.1;
proxy_read_timeout 360s;
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-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8090;
}
}
Pocketbase is started with the default localhost settings on the VPS.
I can even access pocketbase over http://127.0.0.1:8090/api/ when i'm connected via SSH in VS Code and see the requests in the log. (i am surprised that this is even possible. At first i tought i had pocketbase running on my local machine but when i killed the backend on my vps i couldn't access it anymore)
I hope that somebody can help me out as i can't find much about this in the internet.
Problem solved. It works when append a / to the address at the proxy_pass directive
proxy_pass http://127.0.0.1:8090/;

Two locations not working in nginx for MERN application

I have nginx configuration like this:
server {
listen 80 default_server;
listen[::]:80 default_server;
server_name _;
root /var/www/html/ericwu-trademarket/frontend/build;
location /backend/ {
proxy_pass http://localhost:8000; #backend in node js
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 / {
try_files $uri /index.html; #front end in react js
}
}
the front end is running properly. But by running backend like this http://server-ip-address/backend it is showing cannot get /backend/.
Where might I be mistaken?
Check UFW port Allow in server
Check status of UFW:
sudo ufw status verbose
If not show 8000/tcp as a Allow then allow it:
sudo ufw allow 8000
Obviously you are trying to use Websokets.
When it comes to best practices, is better to have the backend services defined inside an upstream definition. You are trying to proxy requests to "localhost:8000" but localhost translates to ip 127.0.0.1. If that is not the ip address of the nodejs app, then is pretty normal that your config won't work.
Nginx expects a fully qualified domain name (FQDN), or ip addresses list of backend servers to work properly.
That being said, your config should be:
http {
upstream backend_server {
#least_conn; #Loadbalancing method in case you want to use multiple backends
#ip_hash;
server backend1.example.com:8000; #or IP address
}
server {
server_name _;
listen 80 default_server;
listen[::]:80 default_server;
root /var/www/html/ericwu-trademarket/frontend/build;
location / {
try_files $uri /index.html;
}
location /backend {
proxy_pass http://backend_server;
proxy_redirect off;
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-Forwarded-Proto $scheme;
# WebSocket specific
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# For long running HTTP requests, don't buffer up the
# response from origin servers but send them directly to the client.
proxy_buffering off;
}
}
}

"This page can't be reached" on Nuxt deployment

I'm trying to deploy a Nuxt App on Digital Ocean over Nginx with pm2.
When I go to the droplet IP I can see the Nginx welcome page but when I try to go to the IP:PORT of the Nuxt App it says:
This site can’t be reached
167.xx.xxx.xxx refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
I have added this on sites-available/default file:
server {
server_name domain.com;
location / {
proxy_pass http://localhost:7200;
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 this I checked for nginx status and restarted it.
Also, I added the service on pm2 with pm2 start --name="webapp" npm -- start and I can see it running correctly.
Even I have tried running directly npm run start command but I get the same result.
Hope you can help me.
server {
listen 80;
server_name localhost;
location / {
root /app;
index index.html;
try_files $uri $uri/ /index.html;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Gitlab and NGINX setup

I am trying to configure an existing NGINX to work with Gitlab omnibus on CentOS. I currently have another application (App A) installed that uses 127.0.0.1:3838. So far I have NGINX setup so that going to my site IP 12.345.678.910, I am able to redirect to App A. I would like to setup Gitlab so that when I go to 12.345.678.910/gitlab, it redirects me to Gitlab. The idea is to run Gitlab on http://127.0.0.1:8081, and have NGINX redirect 12.345.678.910/gitlab to localhost:8081.
I've followed these links for help:
https://docs.gitlab.com/omnibus/settings/nginx.html#using-a-non-bundled-web-server.
Forwarding to GitLab Subdomain with Existing Nginx Installation
Edited /etc/gitlab/gitlab.rb
external_url = 'http://127.0.0.1:8081'
nginx['enable'] = false
web_server['external_users'] = ['nginx']
New config file /etc/nginx/sites-enabled/gitlab
upstream gitlab-workhorse {
server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}
server {
listen 0.0.0.0:8081;
listen [::]:8081;
server_name localhost;
server_tokens off;
root /opt/gitlab/embedded/service/gitlab-rails/public;
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
client_max_body_size 0;
gzip off;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://gitlab-workhorse;
}
}
Added to /etc/nginx/conf.d/default.conf:
server {
listen 80 default_server;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /gitlab {
proxy_pass http://127.0.0.1:8081;
}
I've added 'nginx' to gitlab-www group. Ran the nginx restart and gitlab reconfigure commands.
sudo usermod -aG gitlab-www nginx
sudo service nginx restart
sudo gitlab-ctl reconfigure && gitlab-ctl restart
I installed Passenger per comment in the link above, but that didn't solve the issue. So when I go to 12.345.678.910/gitlab I get a Page Not Found error.
I am still new to all this and any help would be appreciated.

meteor loginButtons not working behind nginx

I'm running a meteor app on port 3000, not as root, and want nginx to redirect port 80 to port 3000.
I've followed the advice here.
This partly works, the front page comes up ok, but {{loginButtons}} does not render. I am using accounts-password with accounts-ui-bootstrap-dropdown.
The only changes that I've made to the default nginx config are these lines in the server section.
location ~* "^/[a-z0-9]{40}\.(css|js)$" {
root /home/ubuntu/bundle/programs/client;
access_log off;
expires max;
}
location ~ "^/packages" {
root /home/ubuntu/bundle/programs/client;
access_log off;
}
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;
}
The console reports an uncaught SyntaxError. Any suggestions?

Resources