如何合并两个nginx配置

时间:2015-07-22 14:19:07

标签: drupal nginx

我有2个nginx配置。他们两个都很好。其中一个是drupal(前端),其中一个是后端。最后我想要mydomain.com/ = frontend& mydomain.com/backend =后端。 不幸的是,我不知道我需要做些什么来完成这项工作。

这两个配置:

//Drupal
upstream fcp {
      server unix:/var/run/php-fpm.sock;
}


server {
        listen 80;
        server_name www.example.com;
        root /usr;
        location / {
                index index.php;
        }

        location ~ \.php$ {
                fastcgi_pass fcp;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /usr$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
        }
}


// ====== //


// Backend-Software
upstream backend{
      server unix:/var/run/php-fpm.sock;
}
server {
    [...]

        server_name www.example.com;
        root /usr/backend/wwwroot;

        #Generic definition
        location / {
                index  index.php;
                try_files $uri $uri/ /index.php?route=$uri&$args;
        }

        #backend.net frontend controller (redirect these calls to PHP-FPM)
        location ~ ^/index.php {
                fastcgi_pass backend;
                fastcgi_param SCRIPT_FILENAME /usr/backend/wwwroot$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }

        # Internal link to the files for X-Accel support
        location /_files {
                internal;
                alias /usr/backend/_files/filesource/;
        }

        #Static Resources
        location /wwwres/ {
          expires 365d;
                alias /usr/backend/wwwres/;
        }
        location ~ ^/wwwres/mod/([^/\.]*)/(.*)$ {
          expires 365d;
                alias /usr/backend/modules/$1/wwwres/$2;
        }
        location ~ ^/wwwres/vendor/([^/\.]*)/([^/\.]*)/(.*)$ {
          expires 365d;
                alias /usr/backend/vendor/$1/modules/$2/wwwres/$3;
        }
        location ~ ^/wwwres/theme/set_([^/\.]*)/(.*)$ {
          expires 365d;
                alias /usr/backend/_files/theme_set/$1/$2;
        }
        location ~ ^/wwwres/theme/([^/\.]*)/(.*)$ {
          expires 365d;
                alias /usr/backend/themes/$1/wwwres/$2;
        }
        location ~ ^/wwwres/vendor_theme/([^/\.]*)/([^/\.]*)/(.*)$ {
          expires 365d;
                alias /usr/backend/vendor/$1/themes/$2/wwwres/$3;
        }
        location ~ ^/_cache/(.*)$ {
          expires 365d;
                alias /usr/backend/_cache/$1;
        }

        #Error Pages
        error_page   404 /static/error/404.php;
        error_page   500 504  /static/error/500.php;
        error_page   502 /static/error/502.html;
        error_page   503 /static/error/503.php;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny  all;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您可以将这些配置合并到一个server部分。此部分中的root可以是/usr(从第一部分开始 - 这是正确的吗?)或/usr/backend/wwwroot。您应该决定哪个是默认值(www.example.com):/usr/index.php/usr/backend/wwwroot/index.php。我认为您需要/usr/index.php,因此root应为/usr。并且应该添加

location /backend {
  root /usr/backend;
  ...
}

location /_files和“静态资源”是正确的,因为您在每个部分都使用alias

要点:

http {
  server_name example.com;
  root /usr;
  index index.php;

  location /backend_uri {
    alias /usr/backend; # if "backend_uri"=="backend" (and root is /usr) this section is unnecessary
  }

  location /_files {
    # same as your post
  }

  # Static resources
  # same as your post
}