这篇文章的目的是在编译安装Nginx的同时,安装upload和uploadprogress模块,以及运行Drupal 8所需要的配置。
由于使用的是Raspberry pi 3B,所以系统用的Raspbian,Debian/Ubuntu应该也是差不多的。

下载Nginx以及相关模块

下载Nginx以及PCRE模块并解压。
进入解压后的Nginx目录,执行命令:

./configure --prefix=/etc/nginx --with-pcre=/tmp/pcre-8.39 --sbin-path=/usr/sbin/nginx --with-http_ssl_module --add-module=/mnt/sources/nginx-upload-module --add-module=/mnt/sources/nginx-upload-progress-module

第一个参数是Nginx安装位置,第二个参数是PCRE源文件位置,第三个参数是Nginx启动的位置。
接着执行 make && make install,即可完成编译安装。

Drupal 8运行需要的配置

首先需要在nginx.conf的http上下文里增加一条:

upload_progress proxied 1m;

这条是表示每上传1M就更新进度信息。

接下来就是Drupal网站的配置:

server {
    server_name d8.local.dev;
    root /mnt/apps/d8;
    client_max_body_size 1024m;
    client_body_buffer_size 2048k;

    # 这个地址是用来获取进度信息,proxied是http里配置的信息。
    location ^~ /progress {
        report_uploads proxied;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;
    }

    location ~ (^|/)\. {
        return 403;
    }

    location / {
    # 如果是自定义字段上传的文件,就交由下面的代码处理
    if ($query_string ~ "X-Progress-ID=\d+"){
        rewrite ^(.*)$ /upload;
    }    
        try_files $uri /index.php?$query_string; # For Drupal >= 7
    }

    location /upload {
        # 文件上传成功后,处理文件的页面。index.php是Drupal的入口文件
        upload_pass /index.php;
        # 是否附带QueryString参数
        upload_pass_args on;
        # 临时存放文件的目录
        upload_store /tmp/nginx_upload;
        # 存放上传状态的目录,用于断点续传
        upload_state_store /tmp/nginx_state;
        # 临时目录的权限
        upload_store_access user:rw group:rw all:rw;
        # 提交到后台的字段名
        set $upload_field_name "tmp_file";
        # 文件名
        upload_set_form_field $upload_field_name.name "$upload_file_name";
        # 文件类型
        upload_set_form_field $upload_field_name.content_type "$upload_content_type";
        # 临时路径
        upload_set_form_field $upload_field_name.path "$upload_tmp_path";
        # 文件MD5信息
        upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
        # 文件大小
        upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
        # 原样提交到后台的表单字段,这里表示所有字段都提交给PHP
        upload_pass_form_field "^.*$";
        upload_cleanup 400 404 499 500-505;
    }

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        # PHP 7 socket location.
        fastcgi_pass 127.0.0.1:9090;

    track_uploads proxied 60s;
    }

    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

在Drupal 8里的具体应用,请看我的另一篇文章:Drupal 8 结合Nginx实现文件上传进度,提高上传文件性能

程序员客栈,汇集各路码农,找到你的靠谱技术小伙伴 http://t.cn/RXz4ONT


刚子0808
61 声望6 粉丝

自由职业,Drupal爱好者。