Nginx always return 502 using Homestead - php

I`m using Laravel 5.2 with Homestead and I get this error on every request.
/var/log/nginx/dev.me-error.log
1415#1415: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.10.1, server: dev.me, request: "POST /api/v1/auth/user/login HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "dev.me", referrer: "http://dev.me/"
This is nginx config, but it is reset on every provision, so I don't think that I can change it.
server {
listen 80;
listen 443 ssl http2;
server_name dev.me;
root "/home/vagrant/Code/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/dev.me-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-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/dev.me.crt;
ssl_certificate_key /etc/nginx/ssl/dev.me.key;
}
Update:
I finally found the problem. It turns out that Nginx respond with 502 when validation error occurs.
This method is used for validating the data in the project:
public static function make()
{
if ( ! static::$rules) {
throw new \Exception('Nothing to validate but expected to.');
}
$data = (self::$data ? self::$data : Request::all());
$validator = parent::make($data, static::$rules, static::$messages);
if ($validator->fails()) {
$messages = [];
foreach ($validator->getMessageBag()->getMessages() as $field => $message) {
$messages[$field] = array_pop($message);
}
throw new ValidationException($messages);
}
return true;
}
The problem was array_pop($message) inside foreach that causes a php error (only in Laravel context):
/var/log/php7.0-fpm.log
[31-Oct-2016 11:04:31] WARNING: [pool www] child 22235 exited on signal 11 (SIGSEGV) after 1534.070207 seconds from start
[31-Oct-2016 11:04:31] NOTICE: [pool www] child 22361 started
I could not find what this error means. I found similar errors only for old php versions.
The fix:
foreach ($validator->getMessageBag()->getMessages() as $field => $message) {
$messages[$field] = $message[0];
}
I also tried to use array_shift() but it causes the same error as array_pop().
If someone knows why is this happening, please tell me.

Related

How to configure NGINX with php-fpm on mac

Been facing this issue now for quite a long time. New on nginx so need help
Basically Im trying to configure nginx settings to use my PHP projects. Before I was using Apache (MAMP) to run all my php proj. Now im shifting to nginx. I have configured all possible settings needed in nginx.conf file (have a look below). But still im facing rather the same issue and im not able to correctly address it for some reason.
...nginx default settings
nginx.conf
server{
listen 80;
error_log /usr/local/etc/nginx/log/freespinswizard-error.log;
access_log /usr/local/etc/nginx/log/freespinswizard-access.log;
access_log off;
#location / {
root /Applications/MAMP/htdocs/freespinswizard/;
index index.php index.html index.htm;
#}
#root /Applications/MAMP/htdocs/freespinswizard/;
#index index.php index.html index.htm;
server_name www.freespinswizard.com freespinswizard.com;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|otf)$ {
access_log off;
log_not_found off;
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/tmp/freespinswizard.sock;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
#fastcgi_buffer_size 128k;
#fastcgi_buffers 256 16k;
#fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 256k;
include fastcgi_params;
add_header X-Frame-Options SAMEORIGIN;
}
rewrite ^/offers/([^/\.]*)/?$ /page-handler.php?story-page-slug=$1 break;
rewrite ^/ab/([^/\.]*)/?$ /page-handler.php?group-key-name=$1 break;
if ($request_uri !~ "(^/scripts/)"){
rewrite ^/.*.php.* /page-handler.php break;
}
if ($request_uri !~ "(^/.*\..*)"){
rewrite ^(.*)$ /page-handler.php break;
}
}
......nginx default settings continued
Error im facing in log file regarding the website im trying to host
2020/11/04 20:14:49 [error] 21300#0: *1 kevent() reported that connect() failed (61:
Connection refused) while connecting to upstream, client: 127.0.0.1, server:
www.freespinswizard.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000",
host: "freespinswizard.com"
I have no clue what is wrong in my configurations. Please any kind of help would be appreciated, thnx in advance.

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.

Php errors is not showing on Browsers , its showing only in log - php 7.1

server {
listen 80 ;
listen [::]:80 ;
server_name php.local.com;
root /var/www/html/php/;
index index.php;
error_log /var/www/html/php/error.log;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# main codeigniter rewrite rule
location / {
try_files $uri $uri/ /index.php;
}
# php parsing
location ~ .php$ {
root /var/www/html/php/;
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
Its showing error in log but not on browser.
Example of Error :
2019/08/06 01:28:13 [error] 14325#14325: *1 FastCGI sent in stderr:
"PHP message: PHP Parse error: syntax error, unexpected '?>' in
/var/www/html/php/scalar_type.php on line 18" while reading response
header from upstream, client: 127.0.0.1, server: php.local.com,
request: "GET /scalar_type.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "php.local.com",
referrer: "http://php.local.com/"

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 error 502 and 504 (upstream timed out) [duplicate]

I'm running a nginx 1.12 and a php-fpm 7.1 as seperate docker containers on a synology nas and i get a 504 Gateway error if the php-script runs longer than 60s. I've tried already several nginx configuration parameters but the error still exists.
Here is my actual nginx config:
#user www-data;
#group http
worker_processes 1;
error_log /opt/data/logs/nginx_error.log notice;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#keepalive_timeout 30s;
sendfile on;
#tcp_nopush off;
tcp_nodelay on;
#gzip off;
send_timeout 300
server {
listen 80;
server_name "";
root /opt/php;
index index.php;
location /data/ {
sendfile on;
root /opt;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
#fastcgi_buffering off;
#fastcgi_keep_conn on;
#fastcgi_intercept_errors on;
#fastcgi_cache off;
#fastcgi_ignore_client_abort on;
}
location ~ ^/(status|ping)$ {
access_log off;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
}
}
}
The php-testscript:
<?php
sleep(65);
echo "done!";
file_put_contents("/opt/data/timetest.txt", "\nEnd", FILE_APPEND);
After 60s the browser shows up the 504 Gateway Time-out. The php-script is still running and is also writing the text to the file.
Nginx errorlog:
2017/07/22 08:16:32 [error] 8#8: *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: , request: "GET /timetest.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "192.168.0.100:8081"
Has anyone an idea?
The question is probably why does your backend take so long to respond? Not sure about your usecase but normally it's not user-friendly to wait to long for a response.
To answer your question:
I found this link: https://easyengine.io/tutorials/php/increase-script-execution-time/
Add in /etc/php5/fpm/php.ini
max_execution_time = 300
Set in /etc/php5/fpm/pool.d/www.conf
request_terminate_timeout = 300
Set in /etc/nginx/nginx.conf
http {
#...
fastcgi_read_timeout 300;
#...
}
And in your config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
}
And reload services
service php5-fpm reload
service nginx reload

Categories