Nginx的服务从另一台服务器的PHP文件
我想配置nginx从另一台服务器的PHP服务。Nginx的服务从另一台服务器的PHP文件
的文件可以在/样品所在的目录内
快速CGI在端口9000上运行的其他服务器
这里的其他服务器上的是我已经尽力了,这是不以工作此时此刻。
location ~ [^/]\.php(/|$) {
proxy_pass http://192.168.x.xx;
proxy_redirect http://192.168.x.xx /sample;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name)
{
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_read_timeout 150;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
我还需要配置nginx的从同一服务器
提供静态文件,您应该不能使用proxy_*
指令。只有当远程服务器已经呈现页面时(使用HTTP协议请求它),才能使用Nginx作为代理。
这里要代理的事情是一个FastCGI的服务器,而不是一个HTTP服务器。
所以关键是:
fastcgi_pass 127.0.0.1:9000;
在哪里你现在说你想达到IP 127.0.0.1端口900,这似乎是相当错误在FastCGI的服务器。
使用,而不是:
fastcgi_pass 192.168.x.xx:9000;
,并删除proxy_*
东西。
编辑:另外,如@Bart的注释中所述,如果测试文档根中的本地文件与php脚本名称匹配,则不应该使用它。 php文件不在此服务器上。所以删除这个文件。 如果你想申请一些安全检查,你稍后可以将你的非常通用的位置[^/]\.php(/|$)
更改为更具体的内容,如location=/index\.php
或其他一些变体。
您不必使用proxy_
指令,因为它们使用HTTP协议,但在这种情况下使用FastCGI协议。另外,正如评论中所述,不需要if
声明,因为Nginx服务器无法确定远程服务器上的文件是否存在。
你可以试试这个配置:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_read_timeout 150;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass 192.168.x.xx:9000; #not 127.0.0.1, because we must send request to remote PHP-FPM server
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/site/root$fastcgi_script_name;
}
你需要用PHP-FPM服务器上的实际路径替换/path/to/site/root
。例如,如果请求http://example.com/some/file.php
必须由/var/www/some/file.php
进行处理,然后将其设置是这样的:
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
此外,为了使PHP-FPM服务器能够接收来自外部的请求,编辑您的FPM池配置(在Debian它通常位于/etc/php5/fpm/pool.d/www.conf
,在CentOS - /etc/php-fpm.d/www.conf
):
更换
listen = 127.0.0.1:9000
有:
listen = 9000
或:
listen = 192.168.x.xx:9000 # FPM server IP
也许你还需要编辑allowed_clients
指令:
listen.allowed_clients = 127.0.0.1,192.168.y.yy # Nginx server IP
我还需要配置nginx的从同一个服务器提供静态文件
如果我理解正确,而且你想服务stati c文件,Nginx正在开发,那么你可以在你的Nginx配置中添加另一个location
。
以下配置不正是你所需要的:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root {STATIC-FILES-LOCATION};
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass {PHP-FPM-SERVER}:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
所有你所要做的就是用你的静态文件的Nginx服务器上的位置和{PHP-FPM-SERVER}
与PHP-FPM服务器的IP取代{STATIC-FILES-LOCATION}
。
这样你将服务于所有文件而不是这个PHP扩展是从Nginx服务器静态的,所有的PHP文件都将被PHP-FPM服务器解释。
下面是您试图实现的dockerised版本的工作示例 - https://github.com/mikechernev/dockerised-php/。它提供来自Nginx的静态文件,并通过PHP-FPM容器解释PHP文件。在附带的博客文章(http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/)中,我详细介绍了Nginx和PHP-FPM之间的整个连接。
编辑:记住一件重要的事情是Nginx和PHP-FPM服务器中的路径应该匹配。因此,您必须将PHP文件放在PHP-FPM服务器上的相同目录中,与Nginx上的静态文件({STATIC-FILES-LOCATION}
)一样。
一个例子就是让Nginx上的/var/www/
在PHP-FPM上持有你的静态文件和/var/www
来存放你的php文件。
希望这有助于:)
没有必要给/样本路径
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass IP:9000;
fastcgi_index index.php;
include fastcgi_params;
}
对于来自nginx的服务器,您需要使用try_files为静态文件。
location/{
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
// other CGI parameters
}
确保您知道共同的pitfalls。
如果你想从你需要有运行网络服务器和从Nginx的
只是代理通你所说的同一台服务器的意思是另一台服务器访问静态文件吗?与上述配置文件中的其他服务器或服务器相同的服务器? –
另一个可以从一个nginx访问的服务器正在运行 – Anon
您应该在另一台服务器上运行Web服务器(例如nginx或Apache),并且只需从该服务器反向代理。 –