Nginx passing parameters to a PHP file - php

I'm trying to pass all the requests that hits a specific location, to a PHP file with the request URL via Nginx config file, I've almost done this but I cannot pass the URL to the PHP file.
I've tried var_dump($_REQUEST); var_dump($_ENV); var_dump($argv); but returns NULL or emtpy array results.
location /p/ {
try_files $uri $uri/ #php;
}
location #php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/process/internal/t.php; # this script catches all requests
fastcgi_param QUERY_STRING $uri;
include fastcgi_params;
}

Try to move the include fastcgi_params; higher, above the fastcgi_params

Related

Is it possible to have web index outside public html?

Say a website has this folder structure
/index.php
/<public>
dikpic.jpeg
And when someone visits the website I want the physical web root to point to /public,
like mywebsite.com/dikpic.jpeg
(without url rewrites).
This can be achieved with the root /myuser/public; command.
But I also want to load the index.php file from outside this directory:
index /myuser/index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
The problem is with the location try files block that assumes / is the web root :(
Any way around this?
I should also point out that the index.php is the only script on the website.
So I don't really care if any other requests for .php files are ignored. The index.php handles url rewriting stuff...
You can use an additional location block for that, although it doesn't seems an elegant solution to me:
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location = /index.php {
fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /full/path/to/your/index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
Exact matching locations have a priority over regex matching ones, so the first block will be used for any PHP file request except the /index.php. You don't even need to define a root for the second location, setting right SCRIPT_FILENAME FastCGI parameter value will be enough.
Update
I didn't notice the very last sentence of your question, but if you didn't care for any other PHP files, only the second block location = /index.php { ... } will be enough.
You can create symlinks of your index.php here is how to do that.
And as result you will have single index.php to all your websites

Nginx Location Root Overlapped

In this nginx configuration:
server {
server_name site.example.com;
index index.html index.php;
location / {
root /projects/proj1/frontend;
}
location /api/v1.0/ {
root /projects/proj1;
try_files $uri /api/v1.0/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
Why every url that contains site.example.com is served by the first location, even site.example.com/api/v1.0/ ? Many thanks in advance.
EDIT: I've already tried to invert the order, so first /api/v1.0/ and second /, but with no luck.
EDIT2: Trying with curl the response is correct, but chrome keep using caching even in incognito...
That's the expected behavior in nginx. location / basically matches everything. If you want to match / only, use exact location: location = /

Increase fastcgi_read_timeout for single route

I have the following Nginx configuration for forwarding requests to a PHP-FPM backend:
server {
...
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
One specific route in the app needs a slightly longer php max_execution_time setting. I've configured this successfully and verified it works by setting a longer fastcgi_read_timeout in the above config.
However, I don't need this to be applied to every single route. I'm guessing I need a nested location somewhere but nothing I've tried seems to work!
The fastcgi_read_timeout directive does not appear to accept dynamic values, so a separate location block for the special route will be required. Looking at your configuration file, I assume the special route is a unique URI processed by the /index.php script. Something like this should work:
location ^~ /special/route/uri {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 100s;
}
You can use a prefix location with the ^~ modifier (as above) to override the regex location that usually processes PHP files. Alternatively, you can use a regex location, but place it above the existing regex location so that it takes precedence.
See this document for location syntax.

NGNIX - look in multiple directories for a .php file and then excecute it

I have .php files in multiple directories (/jobs/marketing/, /jobs/content/ etc) that need to map cleanly to /jobs/name-of-file.php.
For example hitting the url:
/jobs/digital-marketing
needs to map to:
/jobs/marketing/digital-marketing.php
It's safe to assume the file name of each php file is unique across directories.
my current nginx setup is the following:
location /jobs {
expires max;
add_header Cache-Control public;
add_header Pragma public;
rewrite ^/jobs[\/]?$ /marketing/jobs.php last;
location ~* ^/jobs/([\-a-z0-9]*)$ {
try_files /marketing/jobs/engineering/$1.php
/marketing/jobs/marketing/$1.php
/marketing/jobs/business-development/$1.php
/marketing/jobs/content/$1.php;
}
location ~ ^/.+\.php($|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_read_timeout 3000;
}
}
This seems pretty close, except a plain text version of the file gets downloaded instead of nginx redirecting to it. I think I need to somehow catch the correct file with another location block, but I nothing seems to be working (that might not even be the correct approach).
Any ideas about how to achieve this? Or a better approach perhaps?
Thanks.
I belive that you are using try_files a bit wrong. What it does is
Checks the existence of files in the specified order and uses the
first found file for request processing; the processing is performed
in the current context.
You have one context with location ~* ^/jobs/([-a-z0-9]*)$ and another with location ~ ^/.+.php($|/)
So you are finding files but not processing them through PHP as your processing is in another context.
Therefore I belive you should add php processing in your try_files location.
location ~* ^/jobs/([\-a-z0-9]*)$ {
try_files /marketing/jobs/engineering/$1.php
/marketing/jobs/marketing/$1.php
/marketing/jobs/business-development/$1.php
/marketing/jobs/content/$1.php;
...
fastcgi_param ...;
fastcgi_pass ...;
}
To not write same config for both /jobs and .php locations you can cut it to file and include like you do with include fastcgi_params;
So ok, if your actual location directs to correct files - we're half way there. At this moment nginx is trying to download files - let's try to add another location directing those files to PHP parser - this one works for me:
location ~ ^/.+\.php($|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_read_timeout 3000;
}
Add this piece of code after your location and let me know if this works for you. You can read more about nginx with PHP here

nginx extensionless php url without if statement

I use this config:
root /www/mysite/static
location / {
try_files $uri #php;
}
location #php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/mysite/controller$fastcgi_script_name.php;
}
This will first check if /www/mysite/static has the file. If it can't find the file, then it will run fastcgi on "file.php".
Now if I go to www.mysite.com/asdf, it will try to run fastcgi with /www/mysite/controller/asdf.php, which doesn't exist.
I read that If statement is evil, so what should I do instead?
Finally figured it out:
location /info {
root /usr/share/nginx/www;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/info.php;
}
Actually makes sense now as I've bothered to read the documentation...

Categories