怎么进行以Nginx+PHP的Docker多容器连接

2023-04-14 01:36:00 nginx 连接 容器

怎么进行以Nginx+PHP的Docker多容器连接

要想实现多容器连接,首先要在Dockerfile中进行如下配置:

FROM nginx:1.13.0-alpine RUN rm -rf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/

其次,在nginx.conf中进行如下配置:

upstream php-upstream { server php:9000; } server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass php-upstream; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fixes timeouts fastcgi_read_timeout 600s; include fastcgi_params; } }

最后,使用docker-compose.yml进行容器编排:

version: '3' services: nginx: build: . ports: - "8080:80" volumes: - ./code:/usr/share/nginx/html links: - php php: image: php:7.1.8-fpm volumes: - ./code:/usr/share/nginx/html

然后,只需要在宿主机上运行“docker-compose up -d”命令即可。

相关文章