Nginx位置指令似乎不起作用.我想念什么吗? [英] Nginx location directive doesn't seem to be working. Am I missing something?

查看:58
本文介绍了Nginx位置指令似乎不起作用.我想念什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Nginx设置为我的主要Web服务器,并在其后有两个基于Mochiweb的服务器.某些请求被反向代理到这两个服务器. 现在,我想使用nginx访问phpmyadmin(位于/var/www/nginx-default/phpMyAdmin),但一直说找不到错误404.我在这里遗漏了什么明显的东西吗?

I've set up Nginx as my main web server and have two Mochiweb based servers behind it. Certain requests are reverse-proxied to these two servers. now, I want to access phpmyadmin (located at /var/www/nginx-default/phpMyAdmin) using nginx, but it keeps saying Error 404 not found. Am I missing something obvious here?

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

推荐答案

这里的问题是,只有最佳" location指令按以下顺序使用:

The problem here is that only the "best" location directive gets taken, in this order:

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脚本获取正确的根目录:

Using this ruleset, your /phpmyadmin location directive is beaten out by the regular expression ".php$" location directive, so the former is ignored entirely. Additionally, your php fastcgi directive is hard-wired to your /home/me/dev directory, which means that phpMyAdmin is totally inaccessible. You can use a rewrite to get the correct root for your phpMyAdmin scripts:

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

这篇关于Nginx位置指令似乎不起作用.我想念什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆