【问题标题】:nginx trying to specify different locations not the main rootnginx试图指定不同的位置而不是主根
【发布时间】:2018-02-06 19:12:44
【问题描述】:

我有一个处理不同“网站”的配置,主根处理对主 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

【问题讨论】:

    标签: php nginx


    【解决方案1】:
    1. location /mailtracker 放在location / 上方,因为在检查/mailtracker 之前,nginx 匹配路由/mailtracker/
    2. 您可以在location /mailtracker 中删除location ~ \.php$
    3. try_files $uri $uri/ =404; 添加到location /mailtracker

    编辑:

    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;
        }
    }
    

    【讨论】:

    • 没用,我从 php-fpm 得到 File Not found 。获取 /mailtracker/index.php" 404
    • 有一个index.php
    • 没用,我已经试过了,我做错了。
    • nginx 正在将 /mailtracker/ 放在对 php-fpm 的请求中
    • 对不起,现在我才看到你的编辑,我尝试了上面的配置和主要位置 / 没有工作给 403 Forbiden 和 /mailtracker/ 没有解释 php
    猜你喜欢
    • 2014-07-06
    • 2017-11-17
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多