Nginx + Slim route POST data - php

I have an issue configuring Nginx correctly to work with Slim POST route.
In a post here on StackOverflow it was said that it is not a good practice to rewrite POST request.
Post: nginx rewrite post data
The idea is to have http://someurl.com/scan which is a POST route in index.php and allow only POST requests on the /scan and deny everything else.
index.php:
$app->post('/scan', function(ServerRequestInterface $request, ResponseInterface $response) { ... }
My Nginx conf looks like this:
server {
listen 80;
server_name someurl.com;
index index.php;
root /var/www/maxime/public;
error_log /var/log/nginx/max.err
access_log /var/log/nginx/max.log
location / {
deny all;
}
location = /scan {
limit_except POST {
deny all;
}
rewrite ^/scan$ /index.php last;
}
location ~ index\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
fastcgi_pass php_pool;
fastcgi_keep_conn on;
}
}
To me this conf tells me that if a request is done on /scan and it is a POST request then we "redirect" everything to /index.php. Then the index.php location block comes into action which passes everything to the index.php or am I mistaking somewhere?
Currently with this setup I get this error in Nginx:
*100778 readv() failed (104: Connection re
set by peer) while reading upstream client: 123.123.123.123, server: someurl.com,
request: "POST /scan HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "someurl.com"

Related

file not found for localhost sub-domain nginx wordpress

In the browser, I get "File not found". Server log and configuration are provided. I'm really not sure what to do at this point.
The Error:
2018/04/24 14:00:46 [error] 28717#28717: *9 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "wasob.localhost"
wasob.localhost
server {
#Nginx should listen on port 80 for requests to yoursite.com
listen 80;
server_name wasob.localhost;
#Create access and error logs in /var/log/nginx
access_log /var/log/nginx/default.access_log;
error_log /var/log/nginx/wasob.error_log error;
root /var/www/html/wasob/;
#The homepage of your website is a file called index.php
index index.php;
location / {
try_files $uri $uri/ index.php
}
#Specifies that Nginx is looking for .php files
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
#if (!-f $document_root$fastcgi_script_name) {
# return 404;
#}
fastcgi_index index.php;
# SCRIPT_FILENAME parameter is used for PHP FPM determining
# the script name. If it is not set in fastcgi_params file,
# i.e. /etc/nginx/fastcgi_params or in the parent contexts,
# please comment off following line:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
# include the fastcgi_param setting
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
The error and config are available at https://gist.github.com/skinuxgeek/4d4f86490f87805d1781782670551db9

nginx sub location with php-fpm config failed

I have an nginx config for my server as follow, I want
when I request service/dev/sync/sync
it will route to /home/work/app-web/src/api/dev/sync.php/sync
match the php rule with $fastcgi_script_name is /dev/sync.php and $fastcgi_path_info is sync
this rule only need for ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$.
But when I request /service/dev/sync/sync, it will return 404 with error
*173 open() "/home/work/nginx/html/dev/sync.php/sync" failed (2: No such file or directory), client: 172.18.17.90, server: localhost, request: "POST /service/dev/sync/sync HTTP/1.1"
/home/work/nginx/html is the default root for my nginx server. the root config is not
work at all.
what's the problem with this config?
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api;
set $file $1;
set $action $2;
try_files $uri $uri/ /$file.php$action$is_args$query_string;
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $work_dir/src/api$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
}
}
Root config works fine.
Your problem is that there is an internal redirection to /dev/sync.php/sync?, and the new inner-url does not match the regexp-location
You can activate the debug with
error_log /var/log/nginx/debug.log debug;
The logs look like this :
*1 trying to use file: "/dev/sync.php/sync" "/home/work/app-web/src/api/dev/sync.php/sync"
*1 internal redirect: "/dev/sync.php/sync?"
*1 rewrite phase: 1
*1 http script value: "/home/work/app-web"
*1 http script set $work_dir
*1 test location: ~ "/service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$"
*1 using configuration ""
The new URL is /dev/sync.php/sync?, and this url matches with no location, because the regexp /service/([a-zA-Z]/[a-zA-Z])(/[a-zA-Z]*)$ is not intended for the inner-URL.
I suggest you another configuration :
server {
set $work_dir /home/work/app-web;
listen 80;
server_name localhost;
location ~ /service/([a-zA-Z]*/[a-zA-Z]*)(/[a-zA-Z]*)$ {
root $work_dir/src/api/;
set $file $1;
set $action $2;
rewrite .* /src/api/$file.php$action last;
}
location ~ \.php {
internal;
root $work_dir;
fastcgi_split_path_info ^(.+\.php)/(.*)$;
fastcgi_pass unix:/home/work/app-web/php/var/run/app-web.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param UNIQUE_ID $request_id;
include fastcgi_params;
}
error_log /var/log/nginx/test-error.log debug;
}
Note the internal; directive

Symfony + nginx virtual host configuration not working

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

create a block in nginx for symfony2

server {
listen 80;
server_name myapp.local
root /home/jack/Documents/projects/php/myapp/web;
location / {
#try_files $uri $uri/ /app_dev.php?$query_string;
#try_files $uri /app_dev.php$is_args$args;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
access_log /var/log/nginx/myapp-access.log;
error_log /var/log/nginx/myapp-error.log;
}
I've been trying to make this work but I couldn't and when I go to myapp.local I got No input file specified.. In the logs I have:
[error] 6867#0: *1 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nginx/html/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: myapp.local, request: "GET /app.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "myapp.local"
UPDATE
It looks like it's trying to access /usr/share/nginx/html/app.php, why in the world is it doing that?
nginx -v : 1.4.6
Believe it or not guys, after 4 hours of debugging I realized that it was all about a poor missing semicolon after server_name statement. What a shame!

nginx error "rewrite or internal redirection cycle" on CakePHP Project

I have created a default CakePHP 2.4 project and created the Nginx configuration as follows but I am getting these errors from Nginx error log. What I have done wrong?
My environment:
Debian (wheezy)
nginx/1.2.1
project directory /var/www/backhaus
The error is:
$ 2014/06/14 09:39:22 [error] 5952#0: *1 rewrite or internal redirection cycle while processing "/backhaus", client: xxx.x.x.xx, server: azazel, request: "GET /backhaus HTTP/1.1", host: "azazel"
Here is my config from ../sites-available/default:
location /backhaus {
root /var/www/backhaus/app/webroot/;
index index.php;
rewrite ^/* /backhaus;
location ~ ^/(.+\.php)$ {
try_files $uri $uri/ =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Your error is this:
$ 2014/06/14 09:39:22 [error] 5952#0: *1 rewrite or internal
redirection cycle while processing "/backhaus", client: xxx.x.x.xx,
server: azazel, request: "GET /backhaus HTTP/1.1", host: "azazel"
And the error clearly states:
…redirection cycle while processing "/backhaus"…
So now, let’s look at your posted Nginx config:
location /backhaus {
root /var/www/backhaus/app/webroot/;
index index.php;
rewrite ^/* /backhaus;
location ~ ^/(.+\.php)$ {
try_files $uri $uri/ =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
It seems like the issue is your location is /backhaus but then you are redirecting any traffic going to /backhaus to /backhaus via this line:
rewrite ^/* /backhaus;
So I would recommend just removing that rewrite line.

Categories