Nginx with fastcgi_param caches value for other virtualhosts - nginx

When I try to auto_prepend a file in one of my virtualhosts I think it is cached by FastCGI and used on another virtualhost as well. The first code block is one of my sites auto prepending a file:
server {
listen 80;
server_name www.site1.com;
root /var/www/site1;
index index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "auto_prepend_file=/file.php";
include fastcgi_params;
}
}
The second site does not auto prepend the file but actually does so anyway if site1 is requested in any way before site2. It seems like the "fastcig_param PHP_VALUE" is cached somehow for both sites.
server {
listen 80;
server_name www.site2.com;
root /var/www/site2;
index index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Related

Nginx, location, alias and php

We are moving from lighttpd to nginx for HTTP/2.0 and having hard time configuring something which was so simple in lighttpd.
alias.url = ("/api" => "/web/myserver.com/develapi/")
We do this as we version control api and then just point url to appropriate folder. We do not want to use file links as well. Moreover, it's an one line command in lighttpd.
Here is how we are trying to achieve the same in NGINX.
location /api {
alias /web/myserver.com/develapi;
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
However, this does not work as nginx passes /web/myserver.com/develapi as $document_root and /api/index.php as $fastcgi_script_name and hence resulting SCRIPT_FILENAME is wrong.
We then tried to use root
location /develapi {
root /web/myserver.com;
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
This works as nginx passes /web/myserver.com as $document_root and /develapi/index.php $fastcgi_script_name and hence resulting SCRIPT_FILENAME is correct.
However, using root means either we have change existing structure or url. For this situation, using alias is the right solution but the way nginx implementing it, makes it tricky to use alias.
May be we are missing something trivial, any pointers will be appreciated.
I think problem is commonly answered/documented way to return SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Reading more docs reveals $request_filename is a better way to go and works in both root and alias
fastcgi_param SCRIPT_FILENAME $request_filename;
It has been 3+ years... hope you have found the answer by now!
Here is a solution, for similar problem where I wanted to rewrite the fastcgi_script_name stripping off "wiki/" in front of them.
location /wiki {
alias C:/opt/mw/mediawiki-1.36.1;
index index.html index.php index.htm;
location ~ ^/wiki(/.*\.php(?:\?.*)?)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$1;
include fastcgi_params;
}
}
Got the inspiration from: How to remove a path segment from NGiNX fastcgi_script_name?

install postfixadmin in a subfolder of a domain in nginx

I need to install postfixadmin as a subfolder of a domain hosted in nginx.
In other words, I'm trying to access postfixadmin using http://example.com/postfixadmin
Physically, the contents of the sites are stored this way:
example.com site in /var/www/example
Postfixadmin files in /var/www/postfixadmin
I've tried adding this within the server section of example.com:
location ~ /posfixadmin/ {
root /var/www;
index index.php;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
The above works partially, php scripts are executed correctly, but css and image files found under /var/www/postfixadmin/css and /var/www/postfixadmin/images are not loaded.
I've checked at the generated html code and the links to css and image files in postfixadmin are called using relative paths, like this:
href="css/default.css"
I think nginx tries to get the css files from http://example.com/css instead of http://example.com/postfixadmin/css, that's why it's failing, I've tried something like this:
location /postfixadmin/css/ {
root /var/www/postfixadmin;
}
but the above doesn't work.
Any idea about how to fix this? Thanks in advance!
I know it's an old topic, but still: do not use "root" in a "location" block. Source: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
This works for me at the moment with the current PostfixAdmin 3.2 (which moved all the public facing stuff into the "public" subdirectory). Mind that I have defined the fastcgi_pass elsewhere, so this bit is not directly applicable.
location /postfixadmin {
alias /usr/local/www/postfixadmin/public;
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_read_timeout 180;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
}
location ~ \.php {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass php;
fastcgi_index index.php;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Roundcube on Nginx + php-fpm

I am running a VPS server with Centos and Plesk.
The server is working right, on a Nginx + php-fpm setup.
So, websites are served correctly, but when user tries to access to its webemail ( roundcube tool installed ), doesn't work.
My current nginx conf for webmail is :
server {
listen [my server ip...]:80;
server_name webmail.* roundcube.webmail.* horde.webmail.* atmail.webmail.*;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_read_timeout 90;
location / {
root /usr/share/psa-roundcube;
index index.php index.html index.htm;
location ~ \.php$
{
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
}
}
}
What can be wrong?
This snippet works for me on CentOS 6.5. The SCRIPT_FILENAME is different and the is fastcgi_index is present. Think that's it.
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
I have copied it from here and it works. The other directives in this particular example are very worth to get noticed/copied to harden the plesk setup just a little.

lemonstand in subdirectory nginx

I'm running Lemonstand under the subdirectory www.mysite.com/shop/
Here is my location rule for lemonstand:
# Lemonstand
location /shop {
root /home/sites/mysite.com/public_html/shop/;
index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME index.php;
fastcgi_param QUERY_STRING url=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 64k;
}
I can access the page at mysite.com/shop.
All the URLS for the shop should be like this:
mysite.com/shop/category/freight
mysite.com/shop/products/dog-toy
When in fact on the page they are structured like this:
mysite.com/category/freight
mysite.com/products/dog-toy
And the strange thing is, even if I paste in the correct URL to the browser, it only ever shows my base /shop/ page, as if the other pages don't exist. Can anyone help?
You need to serve php from a different location block.
[EDIT] to avoid conflicting with other php scripts, you may use named location.
location ^~/shop/ {
root /home/sites/mysite/public_html;
try_files $uri $uri/ #anotherPhpSite
}
location #anotherPhpSite {
include fastcgi_params;
# Defend against arbitrary PHP code execution
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# More info:
# https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root/shop/index.php;
fastcgi_param SCRIPT_NAME /shop/index.php;
fastcgi_param QUERY_STRING url=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 64k;
}
My main problems were expression syntax and config structure.
OK. So here's the working config file. best practice was to split up the two locations, and utilize one php block for them both.
First the main controller block, which uses silverstripe CMS, here i'm passing the data straight to the main controller in sapphire.
# Main silverstripe declaration
location / {
index /sapphire/main.php;
try_files $uri $uri/ /sapphire/main.php?url=$uri&$args;
}
Second was my shop under a sub-directory which ran lemonstand. It was mainly a case of getting the expressions correct, then passing the requests to the relevant path. I didn't set another root or alias here, as the rule was inherited from the re-write.
# Shop lemonstand files
location /shop/ {
index index.php;
rewrite ^/shop(.*)$ /shop/index.php?q=$1 last;
try_files $uri $uri/ /shop/index.php?q=$1;
}
And finally I used a generic PHP block so I didn't have to duplicate my code in the first two. So any thing that goes through the first two block will pass into here.
# PHP controller
location ~ ^(.*?\.php)(/.*)?$ {
fastcgi_split_path_info ^(.+?\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 64k;
}

Configuration for lithium on nginx

I would like to deploy lithium on nginx server, however there are configurations provided only for Apache and IIS.
I've successfully written several nginx server configurations for various applications in past, but I'm struggling with this one.
Already asked this question on nginx and lithium forums, no luck.
This is best of what I've made so far.
root /var/servers/my_app/app/webroot;
location / {
index index.php;
try_files $uri $uri/ index.php;
}
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
Problem is on / (root page) every link gets index.php prepended, e.g. instead of
www.example.com/something
I get
www.example.com/index.php/something
Not sure if this even is nginx configuration related or rather something, that lithium does when it cannot detect Apache/IIS environment. Either way I cannot solve it.
Another thing, that when I access "www.example.com/test" (via direct URL input), the page renders correctly, however "www.example.com/test/" (with trailing slash) and "www.example.com/test/anything_here" is broken - all links gets appended to current URL e.g. pressing the same link creates this:
www.example.com/test/
www.example.com/test/test
www.example.com/test/test/test
EDIT: Updated configuration
(Sorry for much delayed edit, but I'm still stuck and recently restarted solving this)
root /var/server/my_app/app/webroot/;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~/\.ht {
deny all;
}
}
As I mentioned in comments this now causes all links to have index.php included, it looks like:
www.example.com/index.php/something
www.example.com/index.php/stylesheet.css
I think your problem is that the try_files shouldn't be inside a location block.
Try the configuration shown here: http://li3.me/docs/manual/configuration/servers/nginx.wiki
I helped define it and have been using it locally and in production. It shouldn't cause any of the issues you're reporting.
Copying it below:
server {
listen IP_ADDRESS_HERE:80;
server_name DOMAIN.COM;
root /var/www/DOMAIN.COM/webroot/;
access_log /var/log/DOMAIN.com/access.log;
error_log /var/log/DOMAIN.com/error.log warn;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
As I suspected the problem was that Lithium relies on some environment variables, in this case - link generation, it uses PHP_SELF, which happened to be incorrect.
Solution:
fastcgi_param PATH_INFO $fastcgi_path_info;
Instead of previously incorrect:
fastcgi_param PATH_INFO $fastcgi_script_name;
So final configuration:
root /var/server/my_app/app/webroot/;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~/\.ht {
deny all;
}
Thanks to rmarscher and mehlah # lithum forums.

Resources