我正在尝试将nginx配置为从另一台服务器提供PHP服务。
这些文件可以位于另一台服务器上/sample下的目录中
Fast 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配置为提供来自同一服务器的静态文件
发布于 2017-07-04 16:33:36
下面的配置完全符合您的需求:
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服务器上的静态文件位置替换为{STATIC-FILES-LOCATION},并将{PHP-FPM-SERVER}替换为PHP-FPM服务器的IP。
这样,您将从Nginx服务器静态地提供所有文件,而不使用。
这里有一个你正在尝试实现的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 one上的静态文件({STATIC-FILES-LOCATION})相同的目录下。
例如,在Nginx上使用/var/www/保存静态文件,在PHP-FPM上使用/var/www保存php文件。
希望这能有所帮助:)
发布于 2017-06-28 17:02:41
您不应使用proxy_* directives。使用Nginx作为代理只有在远程服务器渲染了页面时才能完成(并且您将使用HTTP协议请求它)。
这里你想要代理的是一个HTTP服务器,而不是一个服务器。
所以关键是:
fastcgi_pass 127.0.0.1:9000;你现在说你想访问IP 127.0.0.1端口900上的fastcgi服务器,这似乎是错的。
请改用:
fastcgi_pass 192.168.x.xx:9000;去掉proxy_*的东西。
php编辑:此外,正如@Bart在评论中所述,您不应该使用if来测试文档根目录中是否存在与脚本名称匹配的本地文件。php文件不在此服务器上。所以删除这个文件。如果您想要应用一些安全检查,您可以稍后将非常通用的位置[^/]\.php(/|$)更改为更具体的内容,如location=/index\.php或其他一些变体。
发布于 2017-07-03 15:15:17
您不必使用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;
}您需要将/path/to/site/root替换为PHP-FPM服务器上的实际路径。例如,如果请求http://example.com/some/file.php必须由/var/www/some/file.php处理,则将其设置为:
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;此外,要使PHP-FPM服务器能够接收来自外部的请求,请编辑您的FPM池配置(在Debian上,它通常位于Centos -/etc/php-fpm.d/www.conf上的/etc/php5/fpm/pool.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配置为提供来自同一服务器的静态文件
如果我理解正确,并且你想从服务器上提供静态文件,Nginx正在工作,那么你可以在你的Nginx配置中添加另一个location。
https://stackoverflow.com/questions/44706951
复制相似问题