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.
Related
I try to configure Nginx to distribute my laravel api on a subfolder of my domain
server {
listen 80;
server_name stackoverflow.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
charset utf-8;
location /dev {
alias /home/debian/www/stackoverflow/api/dev/public;
try_files $uri $uri/ /dev/index.php?$query_string;
location = /dev/favicon.ico { access_log off; log_not_found off; }
location = /dev/robots.txt { access_log off; log_not_found off; }
error_page 404 /dev/index.php;
error_log /home/debian/logs/nginx/error_log;
access_log /home/debian/logs/nginx/access_log;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I tried different configuration, with /dev on try_files, without /dev, but always same result "File not found" and in error log :
*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 109.221.246.178, server: xxx, request: "GET /dev/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "xxx"
or
*1 open() "/home/debian/www/xxx/api/dev/public/login" failed (2: No such file or directory), client: xxx, server: xxx, request: "GET /dev/login HTTP/1.1", host: "xxx"
My poor knowledge on Nginx stop there. I know its about a file path issue, but can't understand where...
The configuration works perfectly when I try to set up on location / { }
Thanks a lot for your help !
This is my based conf when i put laravel app in domain subfolder, you can try to add your custom settings on it.
My laravel app root is in "subsite" folder, so you may change "subsite" to "dev" and test it.
location /subsite {
alias /var/www/project/subsite/public/;
try_files $uri $uri/ #subsite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
}
}
location #subsite {
rewrite /subsite/(.*)$ /subsite/index.php?/$1 last;
}
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/"
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.
I'm using Laravel (5.4) Forge for a web app that uploads Vimeo and Youtube videos from S3. In the past, before moving to Forge, this script worked correctly, and also still works correctly with smaller files today.
Now that I'm trying to upload larger files (~1gb), Im receiving a 502 Bad Gateway after just over 1 minute for the php upload script. The rest of the application runs fine.
Specifically, here is the error:
2017/04/24 20:36:48 [error] 2111#2111: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: X.X.X.X.X, server: myserver.com, request: "POST /recordings/vimeo/upload HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.1-fpm.sock:", host: "myserver.com", referrer: "http://myserver.com/recordings"
I have tried:
adding / editing fastcgi directives in the nginx config
upping output_buffering in php
adding the proxy_ and client_max_body items below
Here's my NGINX config:
include forge-conf/myserver.com/before/*;
server {
listen 80;
listen [::]:80;
server_name .myserver.com;
root /home/forge/myserver.com/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'hidden for SO';
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
include forge-conf/myserver.com/server/*;
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/myserver.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 3600;
fastcgi_buffers 8 512k;
fastcgi_buffer_size 512k;
include fastcgi_params;
client_max_body_size 128M;
proxy_buffer_size 256k;
proxy_buffers 4 512k;
proxy_busy_buffers_size 512k;
}
location ~ /\.ht {
deny all;
}
}
include forge-conf/myserver.com/after/*;
What am I missing? I can't seem to figure this out at all. Thank you in advance for the help.
"request_terminate_timeout" turned out to be the issue:
https://laracasts.com/discuss/channels/forge/502-bad-gateway-with-large-file-uploads
I had the same 502 problem and after some debugging discovered that I was hitting a limit inside nginx, not a problem in PHP.
Added the following to my site conf and things seem to be working now:
server{
fastcgi_temp_file_write_size 10m;
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;
#...our rest of config
}
typically you can find nginx config file inside
/etc/nginx/sites-available/default or
/etc/nginx/sites-available/your_domain.com
Created a virtualhost for symfony application in local system
Here is the nginx config file
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_index app.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Statics
location /(bundles|media) {
access_log off;
expires 30d;
try_files $uri #rewriteapp;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
On load of app in browser its throwing an error
502 Bad Gateway Error:No input file specified.
Error caught from error.log file:
FastCGI sent in stderr: "Unable to open primary script: /home/guest/symfony_demo/web/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: local.symfony, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "local.symfony"
Can anyone help me to configure symfony app to app_dev config file.
Any thoughts??
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
This line is the problem. If you remove it, your error disappears. You may then have Opcache problems due to Symfony using symlinks to the current project.
With $realpath_root$fastcgi_script_name, the web server looks at the "real" path for the PHP script based on the root definition in your server block. Nginx must have read permissions to the path for this to work. Your code is in /home/guest/, if nginx is running as "www-data" give it permissions to your directories, or run nginx as the "guest" user (ignoring the security implications of this).
Why don't you start with configuration from official documentation (http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html#nginx) and when that works, you can try to add your custom configuration (caching of sttaic files ...) ?
This should work:
server {
listen 80;
server_name local.symfony;
root /home/guest/symfony_demo/web;
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
This error happens when nginx is not able to find the php-fpm.sock file.
Can you make sure that the php-fpm.sock file is in the path as mentioned. I had to update fastcgi_pass like below
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
as my php-fpm.sock was there