Nginx 从不同的服务器提供 php 文件 [英] Nginx to serve php files from a different server
问题描述
我正在尝试将 nginx 配置为从另一台服务器提供 PHP.
I am trying to configure nginx to serve PHP from another server.
文件可以位于其他服务器上/sample下的目录中
The files can be located within a directory under /sample on the other server
Fast CGI 在另一台服务器的 9000 端口上运行
Fast CGI is running on port 9000 on the other server
这是我尝试过的,目前不起作用.
Here is what I have tried, which is not working at the moment.
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 以提供来自同一服务器的静态文件
I also need to configure nginx to serve static files from the same server
推荐答案
以下配置正是您所需要的:
The following configuration does exactly what you need:
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;
}
}
您所要做的就是将 {STATIC-FILES-LOCATION}
替换为您在 Nginx 服务器上的静态文件的位置和 {PHP-FPM-SERVER}
使用 PHP-FPM 服务器的 IP.
All you have to do is replace {STATIC-FILES-LOCATION}
with the location of your static files on the Nginx server and {PHP-FPM-SERVER}
with the IP of the PHP-FPM server.
通过这种方式,您将从 Nginx 服务器静态提供没有 PHP 扩展的所有文件,并且所有 PHP 文件都将使用 PHP-FPM 服务器进行解释.
This way you will serve all files without the PHP extension statically from the Nginx server and all the PHP files will be interpreted with the PHP-FPM server.
这是您尝试实现的 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 之间的整个连接.
Here's a working example of a dockerised version of what you are trying to achieve - https://github.com/mikechernev/dockerised-php/. It serves the static files from Nginx and interprets the PHP files via the PHP-FPM container. In the accompanying blog post (http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/) I go in lengths about the whole connection between Nginx and PHP-FPM.
要记住的一件重要事情是 Nginx 和 PHP-FPM 服务器中的路径应该匹配.因此,您必须将 php 文件放在 PHP-FPM 服务器上与 Nginx 上的静态文件相同的目录中({STATIC-FILES-LOCATION}
).
One important thing to keep in mind is that the paths in both the Nginx and PHP-FPM servers should match. So you will have to put your php files in the same directory on the PHP-FPM server as your static files on the Nginx one ({STATIC-FILES-LOCATION}
).
一个例子是在 Nginx 上使用 /var/www/
来保存您的静态文件,在 PHP-FPM 上使用 /var/www
来保存您的 php 文件.
An example would be to have /var/www/
on Nginx holding your static files and /var/www
on PHP-FPM to hold your php files.
希望这有帮助:)
这篇关于Nginx 从不同的服务器提供 php 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!