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
另一台服务器上的端口9000上运行了快速CGI
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.
这是您要实现的目标的dockerized版本的工作示例- 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文件与Nginx静态文件({STATIC-FILES-LOCATION}
)上的静态文件放置在PHP-FPM服务器上的同一目录中.
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屋!