且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Nginx提供来自其他服务器的php文件

更新时间:2023-02-05 09:11:02

以下配置完全可以满足您的需求:

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.

希望这会有所帮助:)