dsfliu1129
2017-05-25 20:03
浏览 59
已采纳

使PHP和Nginx Docker镜像协同工作

I tried many many advises, but can't make it working. I want to create a docker-compose.yml file with NGINX-PHP cooperation.

Here's what I made:

version: "2"

services:
  nginx:
    image: nginx:latest
    restart: always
    ports: 
      - "80:80"
      - "443:443"
    links:
      - php
    depends_on:
      - php
    expose:
      - "80"
      - "443"
    volumes:
      - ./www:/var/www/html
      - ./config/nginx/site.conf:/etc/nginx/sites-available/default
      - ./config/nginx/site.conf:/etc/nginx/sites-enabled/default

  php:
    image: php:7-fpm
    restart: always
    volumes:
      - ./www:/var/www/html

Docker images run without errors, but when I want to access Nginx, I get this:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

I tried to log into both images and check volumes. I also checked if I can ping php from nginx. Everything seems to be fine...

Here's my Nginx site configuration:

server {
    server_name ncp-docker;

    listen 80;
    index index.php index.html index.htm;
    root /var/www/html;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /(inc|uploads/avatars) {
        deny all;
    }
}

图片转代码服务由CSDN问答提供 功能建议

我尝试了很多建议,但无法使其正常工作。 我想创建一个 docker -compose.yml 文件与 NGINX-PHP 合作。

这是我做的:

 < 代码>版本:“2”
 
services:
 nginx:
 image:nginx:latest 
 restart:always 
 ports:
  - “80:80”
  - “443:443”
 链接:
  -  php 
 depends_on:
  -  php 
公开:
  - “80”
  - “443”
卷:
  -  ./www:/var/www/html
  -  ./config/nginx/site.conf:/etc/nginx/sites-available/default
  -  ./config/nginx/site.conf:/etc/nginx/sites-enabled/default

 php:\  n image:php:7-fpm 
 restart:always 
 volumes:
  -  ./www:/var/www/html
  
 
 

Docker 图像运行没有错误,但是当我想访问 Nginx 时,我得到了这个:

欢迎来到nginx!< / p>

如果您看到此页面,则nginx Web服务器成功运行 alled and working。 需要进一步配置。

有关在线文档和支持,请参阅nginx.org。 nginx.com提供商业支持。

感谢您使用nginx。

我尝试登录图像并检查卷。 我还检查了我是否可以从 nginx ping php 。 一切似乎都很好......

这是我的 Nginx 网站配置:

  server {
  server_name ncp-docker; 
 
 listen 80; 
 index index.php index.html index.htm; 
 root / var / www / html; 
 
 error_log /var/log/nginx/error.log  ; 
 access_log /var/log/nginx/access.log;
nn location~ \ .php $ {
 try_files $ uri = 404; 
 fastcgi_split_path_info ^(。+ \。php)(/。+)  $; 
包括fastcgi_params; 
 fastcgi_pass php:9000; 
 fastcgi_index index.php; 
 fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; 
 fastcgi_param PATH_INFO $ fastcgi_path_info; 
} 
 
 location~ /(inc | 上传/头像){
全部拒绝; 
} 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzhou5344 2017-05-25 20:23
    已采纳

    You are defaulting to default server configuration, so you need to overwrite the default nginx virtual host:

        - ./config/nginx/site.conf:/etc/nginx/conf.d/default.conf
    

    Use that and remove both sites-enabled and sites-available volumes

    点赞 打赏 评论
  • duanaiguang1960 2017-05-25 20:42

    i think you might be approaching this incorrectly.

    the nginx configuration should not be running PHP directly. PHP is running on a separate container.

    instead, you should have nginx reverse proxy to the PHP app.

    something like this:

    
    server {
        listen 80;
        server_name yourdomain.example.com;
        location / {
            proxy_pass http://php:9000/;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
        }
     }
    

    there's probably some detail you need to add / change... but this is the basic reverse proxy that i cut & paste when i need it, in nginx

    点赞 打赏 评论
  • dougou2937 2017-05-25 21:54

    As Robert suggested you have to overwrite the default nginx virtual host, however, I would like to add that you can also add an extra Dockerfile for your nginx container and avoid using volumes. In your docker-compose.yml you have to change image: nginx:latest with build: ./nginx/. For the Dockerfile something like this should do the trick:

    FROM nginx:latest
    RUN rm /etc/nginx/conf.d/default.conf
    ADD conf.d/ /etc/nginx/conf.d/  #Replace the default nginx virtual host
    

    I find this easier as later on you can easily add more things to the container for example SSL certificates for a HTTPS connection:

    ADD ssl/ /etc/nginx/ssl/    
    

    This will save you using volumes and will make your life easier for future nginx settings.

    点赞 打赏 评论

相关推荐 更多相似问题