How to pre-interpret php in .html files on NGINX? - php

I have found a few threads on this, but the solutions I find do not work. I am trying to get my NGINX server to interpret the php inside of html files before sending the html files to the browser for processing. Here is some code inside my server bracket - inside of my only sites-enabled directory file, "default:"
location ~ \.(php|html|htm)$ {
include snippets/fastcgi-php.conf;
root html;
fastcgi_index index.php;
# With php-fpm (or other unix sockets):
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
include fastcgi_params;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Specifically, the part with (php|hmtl|htm) I was hoping to be the solution. However, php in my .html files are still commented out and not processed.
Not sure where security.limit_extensions parameter is? Here is my fastcgi-php.conf in snippets folder:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
Thanks for any ideas.

I don't know what is inside your snippets/fastcgi-php.conf and if it is additionally limiting the PHP code processing via PHP-FPM somehow (I personally prefer to write PHP handlers locations myself), but at least you'll need to adjust the security.limit_extensions parameter in your PHP-FPM pool configuration file (defaults to .php .phar). If it does not help, append the snippets/fastcgi-php.conf file contents to your question.

Related

Nginx conf to invoke xslt script to generate php AND process with php-fpm in one request?

Consider the following non-working nginx.conf fragment:
server {
...
root /var/www/html/example.com/www;
location /dev/ {
root /var/www/html/example.com;
location ~* \.(pdf|js|css|png|jpg|jpeg|gif|ico)$ {
}
fastcgi_split_path_info ^/dev/(.+)()$;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/example.com/xsltproc.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass php-fpm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(x)$;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
}
location / {
index index.html;
location ~ \.php$ {
...
The objective is to use XSLT to transform .xml into .php and instantaneously serve the generated .php file from the protected /dev/ location all in one step for a fast and easy edit / view cycle.
The xsltproc.php script looks up the resource by $fastcgi_script_name and checks to see if the corresponding .xml or .xsl files are newer than the .php file and, if yes, regenerates it using XSLTProcessor::transformToXML().
When development is complete, the .php files are copied to the www location where they are served in the typical non-xslt manner.
However, the above does not work because, even though location /dev/ matches, location ~ \.php$ is ultimately favored and used such that the .php file is not regenerated. If I temporarily remove the location ~ \.php$ section, the .php file is generated correctly. When the section is restored, the .php file is processed correctly.
How do I configure nginx to invoke xsltproc.php AND process the .php file with php-fpm in the same request?
Meaning I effectively want to invoke php-fpm twice in one request: once for xstlproc.php and once on the generated .php file.
Can an internal redirect be used even though the name of the resource is exactly the same (e.g. /dev/test.php)?
UPDATE:
I have not found a way to invoke php-fpm twice. Adding a rewrite immediately after the xsltproc.php fastcgi_pass call results in a jump for the rewrite without executing the script. No surprise there.
However, I did settle on a solution which was to add:
require($phpfile)
to the end of xsltproc.php to simply include the php file regardless of weather or not it was re-generated and then remove location ~ \.php$. The more I think about it, this is a satisfactory solution. It's only for dev anyway.

Nginx's fastcgi-php.conf snippet is missing

I'm attempting to serve PHP with nginx, i've followed this tutorial successfuly before but on a new server for some reason I get the following error:
nginx: [emerg] open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory)
In fact, the whole snippets directory of the nginx installation is missing.
I've installed PHP with the following commands:
- sudo apt-get install -y php7.0-cli php7.0-cgi php-fpm php-mysql
- sudo systemctl restart php7.0-fpm
I've installed the most up to date nginx that is available - and yet the directory and file is still not present.
How can this be remedied?
Bonus: What could have caused this?
TLDR:
Final version is:
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
i think it depends on the Nginx version you are using.
For Nate's answer, nginx-full will install 1.10.3 for you.
I'm using Nginx 1.12.2 on Ubuntu 16.04, with this version, it doesn't have sites-enabled and sites-available with it; and it also has a different PHP CGI setup.
You can either use Ulad Kasach's solution, or start to use the new way.
Here's an official doc for how to do it: https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
BTW, in above post, you should also replace fastcgi.conf with fastcgi_params.
And add one more line which is in default orignially:
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
These are all new changes with Nginx 1.12.2 :(
Final version is:
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
Ended up having to look at a previous, working, configurations file and replicating it manually. Simply made the snippets directory and added a fastcgi-php.conf file with the following content:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
You'll also need to replace the last line, include fastcgi.conf; with include fastcgi_params;.
I would have recommended to create the file fastcgi.conf; if it was not literally the same file with the additional line fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; in the case of fastcgi.conf
Had me stumped for a moment too. You want to install the nginx-full package on ubuntu as opposed to just nginx. nginx-full contains the bits you were missing.
i am using
nginx/1.4.6
Ubuntu 14.04.5 LTS
and my site config looks like this
server {
listen 3010;
root /usr/share/nginx/docs;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name phpSetup;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
and working nicely .
so i do recommend to update your
config
include snippets/fastcgi-php.conf;
to
include fastcgi_params;
so the location block is
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
HOPE IT HELPS
read more on :
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
thanks,

Nginx rewrite path to query string

I use nginx (php-fpm) and fatfree framework. I need some redirect logic in my routing engine. It looks like:
http://example.net/page/...
Instead of dots there could be something like:
another.net/someurl
http://another.net/?local_query
another.net/path/to/article.html
It breaks nginx logic and I see 404 error.
My nginx config looks simply:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
So when I use:
http://example.net/page/another.net/path/to/article.html
Nginx tries to find file in filesystem with name article.html as I understand. How to write rule to ignore any symbols when there is keyword page after domain?
Solution #1:
Probably it doesn't work because there is no fastcgi_pass directive inside /etc/nginx/conf.d/fastcgi_params.conf.
If so, all you need is simply add it to your \.php$ location. In general case it looks like:
fastcgi_pass 127.0.0.1:9000;
but this line might be different, depending on how PHP-FPM is configured.
Solution #2:
Your current configuration makes Nginx pass request to PHP-FPM only if URI ends with .php.
To make Nginx do it when URI starts with /page/ as well, you should write a new location with almost the same content (except for SCRIPT_FILENAME line) and (important!) put it before existing \.php$ location.
Copying location content would cause a code duplication, and I would recommend write the config in a slightly different way:
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www$my_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
include /etc/nginx/conf.d/fastcgi_params.conf;
location ~ /page/ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name /path/to/file.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
set $my_script_name $fastcgi_script_name;
}
In this case you will have 2 scenarios:
if URI starts with /page/, Nginx will run /var/www/$main_host/www/path/to/file.php.
if URI ends with .php, Nginx will run requested php file, as usual.
Solution #3:
Leave your \.php$ location as is and put the following location before it:
location ~ ^/page/ {
include /etc/nginx/conf.d/fastcgi_params.conf;
fastcgi_param SCRIPT_FILENAME /var/www/$main_host/www/path/to/file.php;
fastcgi_param DOCUMENT_ROOT /var/www/$main_host/www;
fastcgi_param PHP_ADMIN_VALUE upload_tmp_dir=/var/www/$main_host/tmp/upload;
fastcgi_param PHP_ADMIN_VALUE session.save_path=/var/www/$main_host/tmp/sessions;
}
Just replace /path/to/file.php with real path to file which should handle such requests.

Nginx Rewrite Rule for all .PHP files/requests to be parsed by index.php

Is it any magic rewrite (like it does on Apache) for nginx to be able to rewrite URLs like '/submit.php' to be able to process them from index.php? We have lots of 404 not found errors because of the site structure and all previous URLs were like '/addrate.php', '/my_settings.php', '/profile.php' --> there are over 50 files like this and it would be very unprofessional and code-unwise to create a separate .php file for each of these functions, instead of parsing them all through index.php and using the needed classes like we do with the other rewrites.
Can you please find a solution/give us a suggestion today about this?
I think some info about this is here, but I want the exact reversed result:
http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/
This configuration allow you to handle all URL (non-existing files in file system) with one php script placed in /var/www/example.org/htdocs/index.php
server {
listen 80; ## listen for ipv4
server_name example.org www.example.org;
root /var/www/example.org/htdocs;
access_log /var/log/nginx/example.org.access.log;
error_log /var/log/nginx/example.org.error.log;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ #php;
}
# enable running php files under php fastcgi
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs$fastcgi_script_name;
#fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
location #php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs/index.php; # this script catches all non existing URLs
fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
}

How to route .html and .js files as PHP on Nginx FastCGI server?

For web servers using PHP as apache module:
AddType application/x-httpd-php .html .htm
For web servers running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
I have an Nginx server and I want to run .js files and and .htm files as PHP, so I will have full PHP code inside them. Anyone know how to configure the Nginx to do this?
Passing to fastcgi didn't work for me. After a few hours of searching, I have found solution here:
http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/
In short:
since PHP versions > 5.3.8, to make it works, you should add directive to your php-fpm.conf:
security.limit_extensions = .php .html .js
The recognition sign is "Access denied." (notice that it's different from HTTP error 403) when accesssing .html or .js file.
Simple; just change
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
to
location ~ \.(php|html|htm)$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Example for .htm, .html files
location ~ \.htm$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.htm;
include fastcgi.conf;
}
Example for .js files
location ~ \.js$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
just change the extension and port settings if needed
Replying to this thread many years later, I spent three hours trying to solve this issue, I had all the right settings in my location block (php|html|htm) and in PHP-FPM (security.limit_extensions), my problem was that I already had the .html extension in a previous location which was used for some static files, so if anyone else is having issues despite using the right settings, please make sure that you're not making the same dumb mistake I did :)
Tom's answer with the link:
http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/
was really helpful. However, I was using php with php-fpm installed on mac os yosemite w/ homebrew. Changes to the php-fpm.conf file did not take effect until I added the following to my .bash_profile:
# for homebrew php55
export PATH="/usr/local/sbin:$PATH"
For details see:
brew info php55
In macOS 11.5 with PHP 8.0, when I browse http://localhost/index.html, it ends with 403 Forbidden
I had to change file /usr/local/etc/php/8.0/php-fpm.d/www.conf to set
security.limit_extensions = .php .html
PHP code blocks in my .html files are properly executed, following is my server block in nginx .conf file
server {
listen 80;
server_name example.local;
root "/var/www/html";
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.html$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Categories