Nginx的位置指令似乎并不奏效。 我缺少的东西吗?(Nginx location direct

2019-06-23 13:41发布

我已经成立了Nginx的作为我的主服务器,并且有它背后的两个基于服务器了Mochiweb。 某些请求反向代理到这两个服务器。 现在,我想访问的phpmyadmin(位于位于/ var / WWW / nginx的默认/ phpMyAdmin的)使用nginx的,但它口口声声说错误404未找到。 我失去了一些东西明显在这里吗?

server {
    ############### General Settings ####################
    listen   80;
    server_name  localhost;
    access_log  /home/me/dev/wwwaccess.log;

    ############## Document Root #######################    
    location / {
        root   /home/me/dev;
        index  index.html index.htm index.php;
    }

    ############## PHPMyAdmin #######################   
    location /phpmyadmin {
        root   /var/www/nginx-default/phpMyAdmin;
        index  index.html index.htm index.php;
    }

    ############## Proxy Settings for FastCGI Server #####
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/me/dev$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }


    ############# Proxy Settings for Mochi1 ###############
    location /mochi1 {
            proxy_pass         http://127.0.0.1:8000;
            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         3600;

            proxy_buffering off;
        }

    ############# Proxy Settings for Mochi2 ###############
    location /mochi2 {
            proxy_pass         http://127.0.0.1:8001;
            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         3600;

            proxy_buffering off;
        }

    ############# Error redirection pages ################
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /home/me/dev;
    }
}

Answer 1:

这里的问题是,只有“最佳” location指令被采取,顺序如下:

location =  <path>  (longest match wins)
location ^~ <path>  (longest match wins)
location ~  <path>  (first defined match wins)
location    <path>  (longest match wins)

使用这个规则集,你的/phpmyadmin location指令是由正则表达式“打了.php$location指令,因此前者被完全忽略。 此外,你的PHP FastCGI的指令硬连接到您/home/me/dev目录中,这意味着phpMyAdmin是完全无法访问。 你可以使用一个重写来得到正确的根phpMyAdmin的脚本:

location ~ \.php$ {
    set $php_root /home/me/dev;
    if ($request_uri ~* /phpmyadmin) {
        set $php_root /var/www/nginx-default/phpMyAdmin;
    }

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}


Answer 2:

将直接“根”。 较少的指令,需要计算少设置更多的增值经销商。 还有其他的东西(如fastcgi_param DOCUMENT_ROOT),将不会在目前接受的答案正确。 此方法将处理所有然而:

location ~ \.php$ {
    if ($request_uri ~* /phpmyadmin) {
        root /var/www/nginx-default/phpMyAdmin;
    }

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    include /etc/nginx/fastcgi_params;
}


Answer 3:

我现在有了这个挣扎了几个小时,没有上述合作,我的情况下讨论的解决方案(因为我需要的index.php运行的index.php与自变量和其它PHP脚本比的index.php),但终于到达了工作配置如下所示:

    location /php-app {
    passenger_enabled off;
    alias /path/to/php-app/$1;
    index index.php index.html;
    try_files $uri $uri/ @rewrite;
   }

   location @rewrite {
    rewrite ^/php-app(.*)$ /index.php?q=$1 last;
   }

location ~ \.php$ {
    alias /path/to/php-app/$1;
    rewrite ^/php-app(.*)$ $1 last;
    passenger_enabled off;
    fastcgi_pass unix:/tmp/php-fpm.socket;
    fastcgi_index index.php;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /path/to/php-app$fastcgi_script_name;
    fastcgi_intercept_errors on;
    }


Answer 4:

也许它搜索的index.html? 尝试改变,以

location /phpmyadmin {
    root   /var/www/nginx-default/phpMyAdmin;
    index index.php;
}

并添加下面的部分,以避免与案件有关的问题

location /phpMyAdmin {
   rewrite ^/* /phpmyadmin last;
}


文章来源: Nginx location directive doesn't seem to be working. Am I missing something?
标签: nginx