【问题标题】:nginx docker, how to change the public path?nginx docker,如何更改公共路径?
【发布时间】:2020-09-14 10:11:53
【问题描述】:

我想用 docker 配置 nginx。 我的网站结构是这样的:

root
..controller/phpFile1.php
..controller/phpFile2.php
..controller/phpFileN.php
..public/index.php
..public/css/file.css
.. and so on

我已经阅读了各种推荐这种配置的指南:

version: '3.6'
services:
  my-app:
    image: nginx
    restart: always
    volumes:
      - "/path/to/public:/usr/share/nginx/html"
    environment:
      # NGINX-PROXY ENVIRONMENT VARIABLES: UPDATE ME
      - VIRTUAL_HOST=your-website-one.com 
      - VIRTUAL_PORT=80
      - LETSENCRYPT_HOST=your-website-one.com 
      - LETSENCRYPT_EMAIL=your.email@domain.com
      # /END NGINX-PROXY ENVIRONMENT VARIABLES
    expose:
      - 80
networks:
  default:
    external:
      name: nginx-proxy

但是.. 我应该做的是链接泊坞窗内的站点文件夹并指定公用文件夹的路径。 我该怎么做?

【问题讨论】:

    标签: linux docker nginx web


    【解决方案1】:

    在你的 docker-compose.yml 中:

    创建一个块:

    nginx:
      container_name: nginx
      image: nginx:latest
      env_file:
        - .env # If any environment variables used, place them in .env
      ports:
        - 80:80
        - 443:443
      volumes:
        - ./config/nginx/conf.d:/etc/nginx/conf.d # This maps nginx config volume
        - ./public:/public # This maps your local public directory volume
    

    现在,在同一级别,创建一个目录

    ./config/nginx/config.d
    

    并在该级别放置一个文件 local.conf,以便您拥有

    ./config/nginx/config.d/local.conf
    

    图片提供的默认nginx.conf会读取任何.conf文件 除非你覆盖它,这在你的情况下是不需要的。

    在您的 ./config/nginx/conf.d/local.conf 添加或编辑块 server 指定 您想要的根位置:

    server {
        root /public; # That location corresponds to one mapped in docker-compose.yml
        index index.php index.html index.htm index.nginx-debian.html;
        server_name localhost;
    
        location / {
                rewrite ^([^.]*[^/])$ $1/ permanent;
                try_files $uri $uri/ /index.php =404;
                include fastcgi_params;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
        }
        location ~ \.php$ {
            include /etc/nginx/fastcgi.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
    
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_param HTTP_PROXY "";
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }
        location ~ /\.ht {
            deny all;
        }
    
        location ~*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff|css|js|xml|txt)$ {
            expires 7d;
            access_log off;
            add_header Cache-Control "public";
        }
    
        location ~ \.(?:swf|pdf|mov|fla|zip|rar|doc|docx|docm)$ {
            try_files $uri =404;
        }
    }
    

    这只是一个示例 server 块,您必须调整您的需求,指定所需的 root 位置、服务器名称和其他位置。 在此示例中,我假设您的 ./public 目录位于同一级别 - 如果它位于目录树中的较高级别或较低级别,您需要对其进行调整,使其与 docker- 的位置相对应compose.ymldocker-compose.ymlnginx 配置块中提供的配置。

    【讨论】:

    • 太棒了!我会努力的!
    猜你喜欢
    • 2015-08-27
    • 2018-10-19
    • 1970-01-01
    • 2017-11-11
    • 2016-11-10
    • 2017-05-28
    • 2021-12-08
    • 2017-01-20
    • 2023-03-09
    相关资源
    最近更新 更多