nginx尝试指定不同的位置而不是主根

时间:2018-02-06 19:12:44

标签: php nginx

我有一个配置处理不同的"网站"主根处理主URL的请求,并为其指定特定路径,在另一个位置/ mailtracker我处理不同的应用程序并将其指向不同的目录。我的问题是php-fpm找不到第二个位置。

基本上我想让几个地点指向不同的根。

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

    root /var/www/sites/dorero/;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

location /mailtracker {
    alias /var/www/sites/mailtracker/web/;
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 192.168.10.3:9000;
                fastcgi_index index.php;
            include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
 }

    location ~ ^/(index|app|app_dev|config)\.php(/|$) {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #       # With php5-cgi alone:
   fastcgi_pass 192.168.10.3:9000;
    #       # With php5-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }

我尝试使用别名/ var / www / sites / mailtracker / web /指定而不是$ document_root,而且它没有用。

从php-fpm输出到此配置说:

192.168.10.4 -  06/Feb/2018:19:04:49 +0000 "GET /mailtracker/index.php" 404

1 个答案:

答案 0 :(得分:1)

  1. location /mailtracker置于location /之上,因为在检查/mailtracker
  2. 之前,/的nginx匹配路由/mailtracker
  3. 您可以删除location ~ \.php$
  4. 中的location /mailtracker
  5. try_files $uri $uri/ =404;添加到location /mailtracker
  6. 编辑:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/sites;
        index index.php index.html index.htm;
    
        # Make site accessible from http://localhost/
        server_name localhost;
        location @rewriteapp {
               rewrite ^(.*)$ /app.php/$1 last;
        }
    
        location /mailtracker {
            alias /var/www/sites/mailtracker/web;
            set $subfolder "mailtracker/web";
            try_files $uri @rewriteapp;
        }
    
        location / {
                root /var/www/sites/dorero;
                set $subfolder "dorero";
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
    
    
        location ~ ^/(index|app|app_dev|config)\.php(/|$) {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                #
                #       # With php5-cgi alone:
                fastcgi_pass 192.168.10.3:9000;
                #       # With php5-fpm:
                #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root/$subfolder/$fastcgi_script_name;
        }
    }