Laravel 502 Bad Gateway Nginx when it's high Traffic - php

I'm using a Laravel App,below is my nginx configuration code:
server {
listen 80;
server_name domain.com;
root /var/www/project/public;
index index.html index.htm index.php index.nginx-debian.html;
charset utf-8;
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Error Log
2018/06/29 08:41:30 [error] 928#928: *14875 connect() to unix:/run/php/php7.0-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: IP, server: IP, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "IP"
Is something wrong with my config? I'm having a heavy servers with 32GB Ram, SSD and processor: 2x E5-2670 0 # 2.60GHz. I'm using Ubuntu with NGINX.
Kindly let me know, I've changed many servers but not able to get rid off this issue.

Check your php-fpm logs to see if nginx is dying. The error you are getting is Nginx saying that the server it is proxying requests to is not answering. This could be a network issue if they are on two different machines, it could be from there not being enough workers to satisfy all of the incoming requests, it could be from the OOM killer on a VPS killing the process. Just because a server has a ton of RAM doesn't mean it impervious to memory exhaustion. I have had a customer with a Magento site who's end of day reports threw memory errors on a server with similar specs due to the poorly coded plugin.
I just saw that you posted a line from your log. Check your pool.conf for the following:
process.max, pm.max_children, pm.min/max_spare_servers
These are just a few of the things you may need to tweak in a heavy traffic environment.

Related

Every PHP error causes an nginx 500 error instead of showing PHP error data

I get a 502 server error from nginx on every PHP request that has an error in it. This is what shows up in the nginx log for every type of error:
[error] 394#0: *7 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: example.test, request: "GET /nova-api/users/lens/example-lens?search=&filters=W3siY2xhc3MiOiJBcHBcXE5vdmFcXEZpbHRlcnNcXExlbnNVc2VyRmlsdGVyIiwidmFsdWUiOnsiY29sdW1uIjoiMCIsIm9wZXJhdG9yIjoiPSIsImRhdGEiOiJoYXJ2ZXkifX1d&orderBy=&orderByDirection=desc&perPage=25&page=1&viaResource=&viaResourceId=&relationshipType= HTTP/2.0", upstream: "fastcgi://unix:/Users/user/.config/valet/valet.sock:", host: "example.test", referrer: "https://example.test/nova/resources/users/lens/example-lens"
If I run the same code on any other system, I actually get a PHP error in my laravel.log file.
I've tried increasing the number of max PHP children.
I've tried completely reinstalling nginx, PHP, and Laravel Valet.
My valet.conf nginx file which is auto-generated by Laravel Valet:
server {
listen 127.0.0.1:80 default_server;
root /;
charset utf-8;
client_max_body_size 128M;
location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
internal;
alias /;
try_files $uri $uri/;
}
location / {
rewrite ^ "/Users/user/.composer/vendor/laravel/valet/server.php" last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log "/Users/user/.config/valet/Log/nginx-error.log";
error_page 404 "/Users/user/.composer/vendor/laravel/valet/server.php";
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:/Users/user/.config/valet/valet.sock";
fastcgi_index "/Users/user/.composer/vendor/laravel/valet/server.php";
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/Users/user/.composer/vendor/laravel/valet/server.php";
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Manually removing valet by doing sudo rm -rf /Users/myuser/.config/valet/ and then valet install fixed the issue.

Automated nginx proxy with fastcgi backends

I can not figure out, how to connect jwilder/nginx-proxy directly to a fastcgi backend. As i am using docker stack, this is the corresponding compose-file:
php-fpm:
image: some/php-app
working_dir: /var/www/application
environment:
VIRTUAL_HOST: php-fpm.example
VIRTUAL_PROTO: fastcgi
VIRTUAL_PORT: 9000
VIRTUAL_ROOT: /var/www/application/public
This config leads to a 404 error:
2018/10/30 07:58:13 [error] 304: *5357 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.255.0.2, server: php-fpm.example, request: "GET /api/settings HTTP/2.0", upstream: "fastcgi://10.0.0.203:9000", host: "php-fpm.example"
If I understand the situation correctly, something like this is missing:
server {
root /var/www/application/public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
When i place this location file under /etc/nginx/vhost.d/default_location or even under /etc/nginx/vhost.d/{VIRTUAL_HOST}_location, i get a error saying server directive is not allowed here. When i only use the "location block", i get an 502 Bad Gateway Error.
Since i have multiple backends, which are mostly identical, some default config which covers most settings would be great.
Has anyone got something like this working?

Where should the routing settings for Laravel 5.5 on Nginx be put?

I am new to Nginx and have just started to give it a try on Windows using WinNMP I have successfully installed and can play with basic PHP. Then I move on to trying Laravel 5.5. The installation using composer seems success and it gives the laravel error page:
The url I used to call this page is http://localhost/mylaraveltest/public/
I have checked the Laravel error log file but no error message there. I then checked the error message from Nginx and see this:
2018/03/28 11:30:20 [warn] 8560#11828: *21 upstream server temporarily disabled while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /mylaraveltest/public/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9003", host: "localhost"
2018/03/28 11:30:20 [error] 8560#11828: *21 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /mylaraveltest/public/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9003", host: "localhost"
It seems to be the problem related to routing problem. From the official page, it seems that I should add the following:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
The problem is, it does not mentioned where should I put this line in.... and it seems to be so obvious that all guides I found by Google simply just focus on what to add but don't mention where to add this line.
I make a guess and put it inside the server bracket of nginx.conf, but the problem is still there. If this is the wrong place to add, where should it be put? And if this is the correct place, then what else can cause the problem?
The file you are looking for is (usually) located in /etc/nginx/sites-enabled/site00.app
And should contain something like:
server {
listen 80;
listen 443 ssl http2;
server_name .site00.app;
root "/home/vagrant/sites/site00/site/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/site00.app-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/site00.app.crt;
ssl_certificate_key /etc/nginx/ssl/site00.app.key;
}
Where site00.app both in the filename and in the config, is the name and tld of your application.

Nginx + Passenger 403 error

I have a hybrid php/Rails app sitting on one AWS ec2 server. I am hosting a Mediawiki installation and using Rails as a frontend to it. For the Rails app, I am using Passenger as a server. I would like location / to serve the Rails app, and anything at location /w or any .php files to be served by Mediawiki (php5-fpm).
I used to have a working configuration, but it was hacked together and I would like to refactor it.
My current working implementation gives me a 403 Forbidden error when I try to access the Rails app at /.
The error I get (from rails_error.log): 2017/10/24 20:08:31 [error] 14947#14947: *2 directory index of "/var/www/myapp/public/" is forbidden, client: xx.yy.zz.aa, server: myapp.amazonaws.com, request: "GET / HTTP/1.1", host: "myapp.amazonaws.com"
I would like to be able to access only the Rails app at / for now; I am not focused on the php5-fpm configurations yet.
Here are my .conf files:
sites-available/myapp.conf:
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=mw_cache:10m max_size=10g inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 80;
listen [::]:80 ipv6only=on default_server;
server_name myapp.com;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
root /var/www/myapp/public;
passenger_enabled on;
location /w {
alias /var/www/mediawiki-1.28.0;
index index.php index.html index.htm;
charset utf-8;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_cache mw_cache;
fastcgi_cache_valid 200 60m;
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:7777;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
error_log /var/log/nginx/mediawiki_error.log;
access_log /var/log/nginx/mediawiki_access.log;
}
error_log /var/log/nginx/rails_error.log;
access_log /var/log/nginx/rails_access.log;
}
nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
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;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
passenger_root /home/ubuntu/.rvm/gems/ruby-2.3.1#myapp/gems/passenger-5.1.1;
passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.3.1#myapp/wrappers/ruby;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
I have a suspicion it has to do with how Passenger is installed or running, or it could be that I am running Passenger not as www-data but as ubuntu.
/var/www/myapp/ is also owned by ubuntu, though I have tried chown -R www-data /var/www/myapp and chown -R ubuntu:www-data /var/www/myapp to no avail.
Does anyone have any pointers from here?
Thanks.
Your config works for me: the app is started successfully, at least, if I start Nginx as root (how it usually is done).
Note that the user directive from your config tells Nginx what user to run its workers as, it does not specify what user to run the Passenger core as (that is inherited from what Nginx was started with).
My pointers would be as follows:
Usually the first thing to do is to check the logs.
Your config declares logfiles, but doesn't set the top level error log, so you're missing the Passenger log output.
To solve this, move the error_log /var/log/nginx/error.log; to above the http { line in your nginx.conf.
If needed, you can also set passenger_log_level 7; (in the http block) to get very detailed logs.
By changing the log level and observing the result you can also ensure that the config you think is being used, is actually the one that is used, on the URL that you are querying (i.e. you can see requests coming in).
Passenger has some troubleshooting tools, e.g. passenger-status can be used to inspect if it's running successfully. Note that you haven't declared a passenger_pre_start url, so your app won't be started by Passenger until the first request is routed to it.

Laravel Forge Nginx Config for SSL

I usually don't post questions until I've researched it to death on the internet. I create a CSR using Laravel Forge, add the Certificate, activate it, edit the Nginx Config using these resources:
https://stackoverflow.com/questions/26192839/laravel-forge-ssl-certificate-not-working
^curl https://domain.com returns data
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/vgport.com/3042/server.crt;
ssl_certificate_key /etc/nginx/ssl/vgport.com/3042/server.key;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/default-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I do 'service nginx restart' in command line, and go to /var/log/nginx/error.log and see the following error:
'conflicting server name "" on 0.0.0.0:80, ignored'
'conflicting server name "www.domain.com" on 0.0.0.0:80, ignored'
When I visit domain.com, it gets redirected to https://domain.com with 'This webpage has a redirect loop'. Clearly the Nginx redirect isn't working somehow but I've followed all the steps.
Please let me know what additional error logs and information I should post to troubleshoot this issue. Any help would be greatly appreciated, thanks in advance.
okay so the problem was simpler than I thought. I was using the free cloudflare dns pointing which didn't support ssl. I switched to using the namescheap dns and it started working.
After spending some time researching nginx, I'd just like to add that when you add a cert, you have to manually press "Activate it" once downloaded.

Categories