Intermittent 502 Gateway error, Nginx / Passenger / Rails - ruby-on-rails-4

I've been pulling my hair out trying to figure out these intermittent 502 errors that have seem to have been happening since day 1.
This usually seems to only happen in development environment, I assume that it's because I have quite a bit of assets that are being loaded.
This is the error in the browser, (Keep in mind it is always a different file that gets the 502 error, it can be either css, or js, sometimes no files get a 502 error)
GET https://example.com/assets/asset.self-368aecefa09656a2be5c72dc8e685b120363a3df812134cbd2dd998bafa179fd.js?body=1 502
In the nginx error log it shows this
[ 2016-11-21 13:17:43.2054 17184/7efd3bfff700 age/Cor/Con/InternalUtils.cpp:112 ]: [Client 2-66] Sending 502 response: application did not send a complete response
[ 2016-11-21 13:17:43.2112 17184/7efd3bfff700 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 17411, application /home/user/app/public (development)
In the rails development.log it shows
Started GET "/assets/asset.self-368aecefa09656a2be5c72dc8e685b120363a3df812134cbd2dd998bafa179fd.js?body=1" for 64.72.213.76 at 2016-11-21 13:24:24 -0700
Started GET "/assets/profile.self-c0665056b47c3ab674073339b59398e0dd63f2b5080c9418e2401dee223f31c6.css?body=1" for 64.72.213.76 at 2016-11-21 13:24:25 -0700
Started GET "/assets/ratings.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 64.72.213.76 at 2016-11-21 13:24:25 -0700
Here is my nginx.conf
user user user;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 2048;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
index index.html index.htm;
server {
listen 443 ssl;
server_name example.com;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /home/user/app/public;
location / {
passenger_enabled on;
passenger_ruby /usr/bin/ruby;
passenger_buffers 16 32k;
passenger_buffer_size 64k;
passenger_intercept_errors on;
passenger_app_env development;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
client_max_body_size 20M;
fastcgi_buffers 16 32k;
fastcgi_buffer_size 64k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
proxy_buffers 8 16k;
proxy_buffer_size 32k;
server {
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
location = /50x.html {
}
}
}

I just needed to disable some gems that were causing prolbems
commented these lines in my Gemfile:
#gem 'figaro'
#gem 'therubyracer'
#gem 'cocaine'
#gem 'pry'
#gem 'pry-doc'
#gem 'roadie'
#gem 'public_activity'
#gem 'stackprof'
#gem 'activerecord-reputation-system'
#gem 'rails-perftest'
#gem 'ruby-prof'

Related

Nginx " Cannot GET /api " Reverse Proxy

My problem:
I need to do a reverse proxy with Nginx in AWS Linux 2 (Free), and the nginx doesn´t stop throwing me Cannot GET /api ...
Code:
server {
location /api {
proxy_pass http://localhost:3000;
}
}
And nothing the only way this works is with
server {
location /api {
proxy_pass http://127.0.0.1:3000/;
}
}
But don't works the /api/something
Here my Nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
some commands used are:
+sudo iptables -A INPUT -i eth0 -p tcp –-dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
+sudo amazon-linux-extras install nginx1 -y

NGINX gives 502 error from browsers but not from Postman

I've got a Django application running on Azure App Service using NGINX.
My nginx.conf file is as follow:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
fastcgi_max_temp_file_size 0;
fastcgi_buffers 128 2048k;
fastcgi_buffer_size 2048k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
server {
listen 8000;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
location /static {
alias /app/staticfiles;
}
}
}
daemon off;
Everything works fine, except for one particular API where I include in the header some token (typical bearer token) and it's returning a 502 error from Chrome (in the network tab)
However, when I try to call this from Postman, it's returning the data correctly.
What could be possibly wrong here?
Thanks to #Selcuk's suggestion. I've managed to fix the above error by increasing the buffer-size in iwsgi.ini file
# uwsgi.ini file
buffer-size = 32768

nginx.conf settting for django

On my ubuntu server i would setting nginx.conf for work with my django app
I setup che nginx.conf file like this:
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 34.233.212.246;
root /usr/share/nginx/html;
#root /home/ec2-user/carrera/podium/static;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:8000;
#proxy_set_header X-Forwarded-Host $server_name;
#proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
add_header P3P 'policyref="/w3c/p3p.xml", CP="IDC DSP COR ADM DEVi TAIi PSA PSD $
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
but when i run service the server return this error:
unexpected end of file, expecting ";" or "}" in /etc/ng$
at the end of the file
Anyone can help me please?
So many thanks in advance
Under location there is the line:
add_header P3P 'policyref="/w3c/p3p.xml", CP="IDC DSP COR ADM DEVi TAIi PSA PSD $
This line does not end with a semicolon (;). That might be causing it?

Deploying django application on nginx server rhel - 400 bad request Request Header or cookie too large

I'm currently trying to deploy a Django app on a REHL 7.4 server using Nginx. I've followed these tutorials :
https://simpleisbetterthancomplex.com/tutorial/2017/05/23/how-to-deploy-a-django-application-on-rhel.html
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
The virtualenv and the nginx server seems to be allright. However I'm struggling with two errors:
Either I got a 500 error because of worker_connections parameter value (below are logs):
13494#0: *1021 1024 worker_connections are not enough while connecting to upstream, client: 192.168.1.33, server: 192.168.1.33, request: "GET /Syc/login HTTP/1.0", upstream: "http://192.168.1.33:80/Syc/login", host: "192.168.1.33"
Either I increase worker_connections value to > 4096 and I get a 400 error like in this thread 400 Bad Request - request header or cookie too large
Below are my nginx.conf and app.conf, please let me know if there are configuration mistakes and thanks in advance for any help.
nginx.conf:
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
# set open fd limit to 30000
worker_rlimit_nofile 30000;
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
large_client_header_buffers 4 32k;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
app.conf
upstream app_server {
server unix:/opt/sycoma/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.33; # <- insert here the ip address/domain name
large_client_header_buffers 4 16k;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /opt/sycoma/logs/nginx-access.log;
error_log /opt/sycoma/logs/nginx-error.log;
location /static/ {
alias /opt/sycoma/venv/Sycoma/Syc/static/;
}
location /media/ {
alias /opt/sycoma/venv/Sycoma/media/;
}
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://192.168.1.33;
}
}
Try to remove/comment the line:
proxy_set_header Host $http_host;
or increase large_client_header_buffers.

nginx: 110: Connection timed out

Sometimes while browsing my website i am getting connection timeout error and not getting any response from server.
my error in nginx error log file is as below
*24 client timed out (110: Connection timed out) while waiting for request, client: 182.59.129.85, server: 0.0.0.0:80
After clearing the cache of the browser it works properly. but after sometime of browsing the issue comes back again.
my nginx configuration file is as below
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx-error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
passenger_root /home/ubuntu/.rvm/gems/ruby-2.0.0-p598/gems/passenger-5.0.8;
passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.0.0-p598/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
# limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# limit_req_zone $binary_remote_addr zone=one:10m rate=2r/s;
#gzip on;
server {
listen 80;
server_name localhost;
root /var/www/application/stag-NRB/current/public;
rails_env staging;
# limit_req zone=one burst=5;
#error_log /var/log/nginx/error.log;
passenger_enabled on;
client_max_body_size 10000M;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
#index index.html index.htm;
#}
location / {
#root /var/www/application/JCI-onsptNew/current/public;
index index.html index.htm;
# limit_req zone=one burst=2 nodelay;
}
location /download_apk {
alias /home/ubuntu;
}
#root /var/www/application/JCI-onspotNew/current/public;
#rails_env production;
#error_log /var/log/nginx/nginx_error.log warn;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
}