2009-06-18 59 views
34

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

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

回答

77

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

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

使用此规则集,您的/phpmyadminlocation指令被正则表达式“.php$”击败指令,所以前者完全被忽略。另外,你的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; 
} 
+5

感谢您的答案!我尝试了很久,但从未回过头。我总是得到HTTP 404错误。直到现在我才明白这个问题。我有/ var/www/nginx-default/phpmyadmin下的phpmyadmin文件。当nginx处理php文件的指令时,它在/ var/www/nginx-default/phpmyadmin/phpmyadmin中搜索第二个phpmyadmin,它是请求中的一个。这是从根目录,它搜索相关的目录,这又是phpmyadmin。所以我不得不在phpmyadmin中创建另一个目录并将所有文件放在那里。现在它工作了! – ErJab 2010-02-10 18:23:07

+3

+1我刚刚烧毁了最近2个小时,试图找出我的nginx.conf中的问题,最后在这里结束......我是nginx的新手,让事情恰到好处是非常令人沮丧的... – dlamotte 2010-05-06 20:18:42

0

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

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

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

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

直接设置'root'。更少的指令,更少的计算需要设置更多的变量。还有其他的东西(比如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; 
} 
2

我现在有了这个挣扎了几个小时,没有上述合作,我的情况下讨论的解决方案(因为我需要的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; 
    } 
相关问题