在Docker中通过Nginx实现PHP的多个版本
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Docker中通过Nginx实现PHP的多个版本,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3378字,纯文字阅读大概需要5分钟。
内容图文
我为PHP5.6运行了两个docker容器:
docker run --name php5 \
-v /html1:/var/www/html/site1 \
-d -p 9001:9000 php:5.6-fpm
而对于PHP7:
docker run --name php7 \
-v /html2:/var/www/html/site2 \
-d -p 9000:9000 php:7-fpm
我用Nginx运行Docker容器:
docker run --name nginx-cache \
-v /nginx.conf:/etc/nginx/nginx.conf \
-v /nginx/html1:/var/www/html/site1 \
-v /nginx/html2:/var/www/html/site2 \
-v /sites-enabled:/etc/nginx/sites-enabled/ \
--link php5 --link php7 -d -p 9999:80 nginx
使用nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
gzip_disable "msie6";
##
# Basic Settings
##
server_names_hash_bucket_size 64;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_body_buffer_size 128k;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
使用site1配置:
server {
listen 80 default_server;
server_name site1;
root /var/www/html/site1/;
index index.php index.html index.htm default.html default.htm;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 180;
location ~ \.php${
fastcgi_pass php5:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $server_name;
}
}
和site2配置:
server {
listen 80;
server_name site2;
root /var/www/html/site2/;
index index.php index.html index.htm default.html default.htm;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 180;
location ~ \.php${
fastcgi_pass php7:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $server_name;
}
}
这个对site2的请求是200 OK,并且响应正确:
curl -X GET \
http://localhost:9999/index.php \
-H 'host: site2'
并且对于site1的请求:
curl -X GET \
http://localhost:9999/index.php \
-H 'host: site1'
在Nginx容器日志中,我总是看到:
2017/04/26 21:18:27 [error] 7#7: *1 connect() failed (111: Connection
refused) while connecting to upstream, client: 172.17.0.1, server:
site1, request: “GET /index.php HTTP/1.1”, upstream:
“fastcgi://172.17.0.3:9001”, host: “site1”
如何解决这个问题的任何想法将非常感激.
解决方法:
好吧,我解决了它,非常错误.出于某种原因,我假设如果我像这样暴露端口9001:
docker run --name php5 \
-v /html1:/var/www/html/site1 \
-d -p 9001:9000 php:5.6-fpm
那么这个端口9001应该用在连接到另一个php5容器的Nginx容器中.这是错误的,因为暴露的网络连接与链接的网络连接不同.
所以正确的site1配置应该是这样的(端口也是9000):
server {
listen 80 default_server;
server_name site1;
root /var/www/html/site1/;
index index.php index.html index.htm default.html default.htm;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 180;
location ~ \.php${
fastcgi_pass php5:9000; # <-- BOOOM!
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $server_name;
}
}
内容总结
以上是互联网集市为您收集整理的在Docker中通过Nginx实现PHP的多个版本全部内容,希望文章能够帮你解决在Docker中通过Nginx实现PHP的多个版本所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。