Setting proper path in Nginx for Symfony2 - symfony

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.

Related

Nginx + WordPress pretty permalinks return 404

Stuck even after having gone through all nginx + WordPress permalinks issue raised here.
I have WordPress installed in a subdirectory digitext-v1. Root directory only has phpinfo file.
root --> /var/www
subdirectory --> /var/www/digitext-v1
Plain permalinks work fine with home and all pages. Pretty permalinks work with home page but gives 404 error for all others.
My conf file is given below. Has almost identical setup (except for fqdn used in server name) to my live Nginx server where pretty permalinks work perfectly fine.
Can you help me resolve it before I lose my sanity? Anything to do with domain name? What am I doing wrong??
server {
listen 80;
server_name digitextv1 192.168.11.8;
root /var/www/digitext-v1;
index index.php index.html index.htm;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location / {
# Setting pretty permalinks in WordPress
# First attempt to serve request as file ($uri)
# or directory ($uri/).
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT (few lines from error log)
2021/08/31 16:05:48 [error] 3185#3185: *1406 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106" while reading upstream, client: 192.168.11.41, server: 192.168.11.8, request: "GET /digitext-v1/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.11.8", referrer: "http://192.168.11.8/digitext-v1/wp-admin/options-permalink.php"
2021/08/31 16:06:28 [error] 3185#3185: *1426 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106" while reading upstream, client: 192.168.11.41, server: 192.168.11.8, request: "GET /digitext-v1/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.11.8"
2021/08/31 16:06:28 [error] 3185#3185: *1426 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106" while reading upstream, client: 192.168.11.41, server: 192.168.11.8, request: "GET /digitext-v1/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.11.8"
2021/08/31 16:09:59 [error] 3185#3185: *1438 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106" while reading upstream, client: 192.168.11.41, server: 192.168.11.8, request: "GET /digitext-v1/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.11.8", referrer: "http://192.168.11.8/digitext-v1/wp-admin/options-permalink.php"
2021/08/31 16:09:59 [error] 3185#3185: *1438 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106PHP message: PHP Warning: preg_match() expects parameter 2 to be string, array given in /var/www/digitext-v1/wp-content/plugins/popup-maker/includes/functions/popups/template.php on line 106" while reading upstream, client: 192.168.11.41, server: 192.168.11.8, request: "GET /digitext-v1/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.11.8", referrer: "http://192.168.11.8/digitext-v1/wp-admin/options-permalink.php"
I had exactly the same issue this week. I even went so far as to reset WordPress, without success. I uploaded my .htacess file to my host advisor Chris at easy.dns and after some back and forth he deleted the following code to solve the problem:
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
<Files *config.json*>
order allow,deny
deny from all
</Files>

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

my nginx doesn't display php file at centos7

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"

Nginx - Setting root location with spaces in path

I'm trying to configure nginx for my project. Step by steps:
Create config file myproject
--/etc/nginx/sites-available
----myproject
Content of myproject file
server {
listen 80;
listen [::]:80;
root "/path/to/html/My Project/company/myproject";
index index.php index.html index.htm index.nginx-debian.html;
server_name myproject.localhost;
location / {
try_files $uri $uri/ /index.php?$request_uri;
location = /index.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME "/media/path/to/html/My Project/company/myproject/index.php";
}
}
}
Restart service nginx and try to load myproject.locahost in browser, I got "File not found."
View latest line in nginx error log, I got below error
9055#9055: *4 stat() "/media/path/to/html/My Project/company/myproject/" failed (13: Permission denied), client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", host: "myproject.localhost"
9055#9055: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "myproject.localhost"
If I move it to a directory path without space, It seems to work.
How can I define this path to make it work?
You should not use path with spaces in file or directory names. But in general Unix file or directory names can contain any character other than / (which is always a directory separator) and null bytes (which you can't use =D). Try to not use spaces, $, ;, |, <, >, etc. in file or directory names, or you should escape them using backslash symbol \ behind escaping symbol.

Nginx Virtual Directory using the same root gives 404 error

I'm fairly new to Nginx, and I'm attempting to import legacy sites from Apache. I want to get rid of the virtual directories but unfortunately I cannot.
The virtual directories point to the same root at the primary site. There is logic in the code that detects the virtual directory and loads data based on that information, which is why it's there.
Here is the config I'm attempting to get to work:
server {
listen 80;
large_client_header_buffers 4 32k;
server_name site.domain.com;
access_log /var/log/sites/site.access.log;
error_log /var/log/sites/site.error.log error;
location / {
root /var/www/php/site;
index index.php;
include /etc/nginx/conf.d/php.inc;
}
location /sitea {
root /var/www/php/site;
index index.php;
include /etc/nginx/conf.d/php.inc;
}
location ~ /\.ht {
deny all;
}
}
Contents of php.inc:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
I have tried every thing that google has to offer to get this to work, but no matter what I do I continue to get the following error:
2018/11/15 20:28:32 [debug] 5056#5056: *1 http script var: "/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "/sitea/index.php" "/var/www/php/site/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "=404" "/var/www/php/site=404"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http finalize request: 404, "/sitea/index.php?requestType=home" a:1, c:1
2018/11/15 20:28:32 [debug] 5056#5056: *1 http special response: 404, "/sitea/index.php?requestType=home"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http set discard body
2018/11/15 20:28:32 [debug] 5056#5056: *1 xslt filter header
2018/11/15 20:28:32 [debug] 5056#5056: *1 HTTP/1.1 404 Not Found
Any help towards the right direction will be appreciated.
Note: it also does the same thing using the alias vs root
With alias I get the following:
2018/11/15 20:37:38 [error] 5189#5189: *1 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: #.#.#.#, server: site.domain.com, request: "GET
/sitea/index.php?requestType=home HTTP/1.1", upstream:
"fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "site.domain.com"
Possible Solution
I'm not sure if this is the proper way of doing it, but it worked.
If I created a symbolic link for the virtual directory in the main folder the site loaded. I'd rather do this in Nginx, but if I have to go this route I will. Thoughts?
The root directive constructs the path to the file by the simple concatenation of its value with the current URI. So your second location block is looking for the file at /var/www/php/site/sitea/index.php.
The alias directive within a prefix location will replace the prefix text with the alias value. See this document for more.
location /sitea {
alias /var/www/php/site;
...
}
So the above location block will look for the URI /sitea/index.php at /var/www/php/site/index.php.
Both the root and alias directives set a variable called $request_filename to the path to the file.
In your PHP block, you use $document_root$fastcgi_script_name to inform PHP-FPM of the path to the file. This works with root but not with alias.
fastcgi_param SCRIPT_FILENAME $request_filename;
The above works with both root and alias for PHP blocks which do not process path info (such as yours).

Resources