my nginx doesn't display php file at centos7 - nginx

I installed nginx at centos7(VPS) I know this question are often asked here. I've been searching solution but my nginx get worse. so I post here.
when first time I tried open a php file broswer asked file download insted of display. I searched solution at web and adjust code. after that.
I can display "Welcome to nginx!" but when I tried to open index.php , I got "An error occurred." That what I made a file that
Could you teach me what is wrong my code please?
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
And this is my log
2019/04/14 03:33:51 [error] 3445#3445: *10 connect() failed
(111: Connection refused) while connecting to upstream,
client: 140.xxx.xxx.x, server: localhost, request:
"GET /index.php HTTP/1.1", upstream:
"fastcgi://127.0.0.1:9000", host: "xxx.xxx.xxx.x"

Related

nginx on WSL2 takes minutes to load the page after few requests

I did read and try the following;
https://stackoverflow.com/a/46286973/8068675 listen = 127.0.0.1:9000;
https://stackoverflow.com/a/50615652/8068675 disable buffering
https://github.com/microsoft/WSL/issues/393#issuecomment-442498519 disable buffering + different config
But none of theses fixed the issue.
Issue
From Windows; when I browse my website located in WSL2 through http://myproject.test, https://myproject.test or 127.0.0.1 the first 2-3 requests are going fast (<100 ms). Then the next requests takes exactly 60000ms (1 minute) to be received when they are not blocked.
Configuration on Windows 10
Firewall disabled
127.0.0.1 myproject.test added to C:\Windows\System32\drivers\etc\hosts
mkcerts installed
WSL 2 installed with Ubuntu 20.04 on it
Configuration on WSL 2
Ubuntu 20.04
nginx 1.18
mysql 8.0
php-fpm 7.4
Project
Laravel
location /home/clement/projects/myproject/
certs (generated with mkcert) /home/clement/projects/certs/
owner: clement:www-data
permission : 777 (It's only for test and development purpose)
/etc/nginx/sites-available/myproject.test
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /home/clement/projects/certs/myproject.test.pem;
ssl_certificate_key /home/clement/projects/certs/myproject.test-key.pem;
client_max_body_size 108M;
access_log /var/log/nginx/application.access.log;
server_name myproject.test;
root /home/clement/projects/myproject/public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_buffering off;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
include fastcgi_params;
}
}
I have the same issue using fastcgi_pass 127.0.0.1:9000; or fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
When I do php -S localhost:8080 or php artisan serve in the project, everything's working fine.
Edit with log
This is the log I'm getting on nginx, but even with this information I still cannot find any resource that fix the issue.
2020/08/07 23:06:30 [error] 1987#1987: *6 upstream timed out (110: Connection timed out) while reading upstream, client: 127.0.0.1, server: myproject.test, request: "GET /_debugbar/assets/javascript?v=1588748787 HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "myproject.test", referrer: "http://myproject.test/"
Or using IP
2020/08/08 01:43:01 [error] 4080#4080: *4 upstream timed out (110: Connection timed out) while reading upstream, client: 127.0.0.1, server: myproject.test, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "myproject.test"
I finally found the issue. Despite I followed the instruction to install WSL 2, it was using WSL 1.
In powershell I ran
wsl -l -v and got the result
|---------------------|------------------|------------------|
| NAME | STATE | VERSION |
|---------------------|------------------|------------------|
| Ubuntu-20.04 | Stopped | 1 |
|---------------------|------------------|------------------|
After updating the kernel I could change the version to 2 with the command
wsl --set-version Ubuntu-20.04 2
and now everything works well

Mount WordPress named volume in subdirectory of NGINX container with docker-compose

The following configuration correctly serves a WordPress instance. (Some unrelated config details omitted for brevity.)
docker-compose.yml
version: "3.7"
services:
nginx:
image: nginx:alpine
links:
- wordpress-1
volumes:
- wordpress-1_files:/var/www/html
wordpress-1:
image: wordpress:php7.3-fpm-alpine
volumes:
- wordpress-1_files:/var/www/html
volumes:
wordpress-1_files:
wordpress-1.conf
server {
server_name wordpress-1.com;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_pass wordpress-1:9000;
}
}
However, I would like to mount the wordpress-1_files volume to a subdirectory (/var/www/html/wordpress-1) in the nginx container (so that I can serve multiple applications from the same NGINX proxy). The configuration below returns a 404 with a File not found. in the response body. This result occurs with either of the two SCRIPT_FILENAME lines in the wordpress-1.conf below.
docker-compose.yml
version: "3.7"
services:
nginx:
image: nginx:alpine
links:
- wordpress-1
volumes:
- wordpress-1_files:/var/www/html/wordpress-1
wordpress-1:
image: wordpress:php7.3-fpm-alpine
volumes:
- wordpress-1_files:/var/www/html
volumes:
wordpress-1_files:
wordpress-1.conf
server {
server_name wordpress-1.com;
root /var/www/html/wordpress-1;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/html/wordpress-1$fastcgi_script_name;
fastcgi_pass wordpress-1:9000;
}
}
When I inspect the NGINX error_log in debug, I see this:
FastCGI sent in stderr: "Primary script unknown" while reading
response header from upstream, client: 555.69.70.132, server:
wordpress-1.com, request: "GET / HTTP/1.1", upstream:
"fastcgi://172.18.0.3:9000", host: "wordpress-1.com"
That leads me to believe that there is an issue with SCRIPT_FILENAME, but neither /var/www/html$fastcgi_script_name nor /var/www/html/wordpress-1$fastcgi_script_name seem to work.
Question:
How can I adjust my second pair of config files so that I can serve WordPress when mounting the WordPress volume to a directory other than /var/www/html in the nginx container?
Note: The above example is a slimmed-down, minimum example of the issue I am trying to resolve. The actual project I'm working on is over here.
Ugh, turns out my testing was just not thorough enough (despite going at this for a few days off and on). The solution was in fact using:
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
This is because inside of the wordpress:php7.3-fpm-alpine image, that is the path where the WordPress files are located.
During my testing, I had forgotten to disable CloudFlare's caching which was sitting in front of my staging site. So, my tests were not always testing what I thought they were...

nginx configuratio : alias and 404 error

My actual problem was that I wanted to make my "site.com/blog/index.php" direct to "/srvX/www/blog/caller/index.php". Althought it would be very straightforward to direct to "/srv/www/blog/index.php" using "root /srv/www/", that's not what I wanted. I discovered "alias", and it seem to do what I want.
1)First try :
server {
listen 80;
server_name _;
root /srv/www/blog/pages;
index index.php;
location /blog {
alias /srv/www/blog/caller;
}
}
There trying site.com/blog get me a 404 not found, and nothing pop into /var/log/nginx/error.log
1)Second try to know what happens :
If I change "alias /srv/www/blog/caller;" to a bad path, let say "alias /srvX/www/blog/caller;" I actually got the same behaviour in my browser, but
I can see in /var/log/nginx/error.log :
[error] 7229#0: *1 "/srvX/www/blog/caller/index.php" is not found (2: No such file or directory), client: 192.168.1.200, server: 192.168.1.221, request: "GET /blog/ HTTP/1.1", host: "192.168.1.221"
Conclusion : I don't know what's hapenning there : it seem clear that nginx get the file in my first try, but it sends the 404 error to the browser with no reason I could think of, while when specyfiyng a wrong path, it tells me right away. :/*
edit
Well, I found the solution. Basically it totally works from nginx, the problem was from php-fpm who lose his mind when using alias into nginx. What you need to do is doing a sublocation of aliased locations adding :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
Now it works.
The fact that nginx was giving a 404 error without anything in the nginx's logs, was that php-fpm was the one failing to serve.
The problem is that you have no instructions on how to deal with the php script. To solve this issue the following:
Add the following code to your nginx.conf file within the server tags or if you have created that in your conf.d folder add it to that file.
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
That will solve that problem but also in the file:
/etc/php-fpm.d/www.conf
Ensure that listen.owner is set to listen.owner = nginx
Ensure that listen.group is set to listen.group = nginx
Restart both services and it should work.
If not ensure your document root and all files with that directory are owned by the user nginx and the group nginx.
If not you can do this by using the following:
chown -R nginx:nginx documentroot
And keep doing that but adding /* each time until you reach an error.
Hope everything works out for you!!

Getting Fatal Errors in Nginx to Be Shown in the Browser

EDIT:
I have done everything suggested in the duplicate answers and nothing works. Here is my PHP info:
This is a specific issue with nginx and stderr messages and is not a generic "how do I turn on PHP error reporting" issue.
While developing I'm getting blank pages in my browser whenever I create a fatal error in PHP with a typo or just my bad programming ;). It's super annoying for me to have to view the raw nginx error log file to see the fatal errors and find the line numbers where they are. I can't seem to find how to make nginx display PHP fatal errors in the browser. Here is the relevant part of my nginx config:
location #fpm {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $path_info;
}
Here's an example error that shows up in my error log and then results in a blank browser page:
2014/01/04 14:53:52 [error] 20082#0: *403 FastCGI sent in stderr:
"PHP message: PHP Fatal error: Cannot redeclare class ClassName in FilePath on line 356"
while reading response header from upstream, client: 192.168.1.10,
server: servername, request: "GET URLPATH HTTP/1.1",
upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "host",
referrer: "referer"
Here is my PHP-FPM conf:
http://pastebin.com/QkCTbBYj
And my PHP-FPM pool conf:
http://pastebin.com/TZfZ8d7G
And my PHP-FPM php.ini:
http://pastebin.com/RsXRxduf
I would love if anyone could shed some light on what I could do to get these errors to show up!
try setting catch_workers_output to yes in pool config:
; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
catch_workers_output = yes
if that doesn't work you might have this bug:
https://bugs.php.net/bug.php?id=61045

Setting proper path in Nginx for Symfony2

I need to have my symfony app installed on the same domain as other webapps so I wanted it to sit in /dev/symfony_app path
I tried to use NginX Friendly PHP Framework but solutions from there do not work.
I have such nginx config and it does not work at all too. there is some problem with paths, nor root neither alias directive work for me.
location /dev/symfony_app/ {
root /home/.../public_html/web;
}
location ~ ^/dev/symfony_app/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
nginx error log:
request http://domain.com/dev/symfony_app/
2013/06/23 11:25:31 [error] 22549#0: *668
"/home/.../public_html/web/dev/symfony_app/index.php"
is not found (2: No such file or directory), client: *,
server: domain.com, request: "GET /dev/symfony_app/ HTTP/1.1", host: "domain.com"
request https://domain.com/dev/symfony_app
2013/06/23 11:25:37 [error] 22549#0: *668
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
request https://domain.com/dev/symfony_app/app_dev.php
2013/06/23 11:27:06 [error] 22549#0: *797
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app/app_dev.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
Well, what are the dots doing there in your path? You can’t have a directory with three dots as name (at least this would be new to me). The error message from nginx is very specific in that regard. That path doesn’t exist.
server {
listen 80;
server_name _;
root /home/public_html/web;
location / {
location ~* ^/dev/symfony_app/(app|app_dev|config)\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
}
That should do the trick.
The absolute local path to the index file of your Symfony installation has to be /home/public_html/web/dev/symfony_app/index.php. A request to http://example.com/dev/symfony_app will map the above location.
I hope this helps, otherwise please leave a comment and describe what else is going wrong.

Resources