基于NGINX部署Drupal时配置文件参考样例

标签:

环境概述:Mac OS X Mavericks, NGINX, PHP, php-fpm, MySQL, phpmyadmin, APC, ionCube loader.

NGINX信息:该系统上Nginx的所有配置文件位于/usr/local/etc/nginx。主要的配置文件是nginx.conf(类似于Apache的httd.conf)。

修改操作:把Web Server配置成类似Apache的虚拟主机,每个站点有一个单独的配置文件。在Nginx的配置目录新建config.d的目录,然后在nginx.conf中引入该目录下的配置文件:

    http {
        ...
       
        # 包含刚才创建的文件夹里的所有配置文件
        include /usr/local/etc/nginx/conf.d/*.conf;
    }

然后在config.d下面创建通用配置文件default.conf(详细文件信息参考文章最下面),其中解决Rewrite的代码如下:

server {
   
    ...
   
    location / {
       ...
       try_files $uri @rewrite;
    }

    ...

    location @rewrite {
            # For Drupal 7
    # 对于Drupal项目位于根目录的情况,e.g /document_root/drupal_project_files
    #rewrite ^ /index.php;
            # 对于Drupal项目位于根目录下子目录的情况,e.g /document_root/sub_dir/drupal_project_files
            rewrite ^/([^/]*)/(.*)(/?)$ /$1/index.php?q=$2&$args;

    # For Drupal 6 and below:
    #rewrite ^/(.*)$ /index.php?q=$1;
    }

    ...
}

//-------------------------------------------------
// 备注:default.conf, 全文      
//-------------------------------------------------
server {
    listen       8080;
    server_name  127.0.0.1;

    #access_log  logs/host.access.log  main;

    # Set the virtual directory.
    #root /var/www;
    root /Users/mook/Sites;
    index  index.html index.htm index.php;

    #error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
       index index.php;
       autoindex on;
     # Rewrite
     try_files $uri @rewrite;
    }

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

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

    # No no for private
    location ~ ^/sites/.*/private/ {
    return 403;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
    return 403;
    }

    location @rewrite {
    # You have 2 options here
    # For D7 and above:
    # Clean URLs are handled in drupal_environment_initialize().
    #rewrite ^ /index.php;
      # Drupal in a subdirectory
      rewrite ^/([^/]*)/(.*)(/?)$ /$1/index.php?q=$2&$args;
    # For Drupal 6 and bwlow:
    # Some modules enforce no slash (/) at the end of the URL
    # Else this rewrite block wouldn't be needed (GlobalRedirect)
    #rewrite ^/(.*)$ /index.php?q=$1;
    }

    # Fighting with Styles? This little gem is amazing.
    # This is for D6
    #location ~ ^/sites/.*/files/imagecache/ {
    # This is for D7 and D8
    location ~ ^/sites/.*/files/styles/ {
    try_files $uri @rewrite;
    }

    location = /50x.html {
       root   html;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_param HTTPS on;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    }

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

    location ~ /\.ht {
       deny  all;
    }
}