【问题标题】:Nginx location directive doesn't seem to be working. Am I missing something?Nginx 位置指令似乎不起作用。我错过了什么吗?
【发布时间】:2010-11-03 21:50:23
【问题描述】:

我已将 Nginx 设置为我的主要 Web 服务器,并在其后面有两个基于 Mochiweb 的服务器。某些请求被反向代理到这两个服务器。 现在,我想使用 nginx 访问 phpmyadmin(位于 /var/www/nginx-default/phpMyAdmin),但它一直说找不到错误 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;
    }
}

【问题讨论】:

    标签: nginx


    【解决方案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;
    }
    

    【讨论】:

    • 感谢您的回答!我很久以前就试过了,但当时它从来没有用过。我总是收到 HTTP 404 错误。直到现在我才发现问题所在。我在 /var/www/nginx-default/phpmyadmin 下有 phpmyadmin 文件。当 nginx 处理 php 文件的指令时,它会在 /var/www/nginx-default/phpmyadmin/phpmyadmin 内部搜索第二个 phpmyadmin 是来自请求的那个。那是从根目录,它搜索相对目录,它又是 phpmyadmin。所以我不得不在 phpmyadmin 中创建另一个目录并将所有文件放在那里。现在可以了!
    • +1 在过去的 2 个小时里,我只是想弄清楚我的 nginx.conf 的问题,最后到了这里……我是 nginx 新手,得到东西非常令人沮丧刚刚好……
    • 很好的答案。我没有意识到 Nginx 总是只选择一个位置,即使不止一个匹配。我不得不承认它在文档中明确说明。
    • 没有if 指令有没有办法做到这一点? Nginx 文档说尽可能避免使用if(尽管在这种情况下可能没有其他好方法)。
    • 对于location = 说“最长的比赛获胜”不是错的吗?如果匹配准确,则只能匹配一个。
    【解决方案2】:

    直接设置'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;
    }
    

    【讨论】:

    • 如果 /phpmyadmin 是 URI (http://host:port/phpmyadmin/...) 的一部分,root 应设置为 /var/www/nginx-default/,除非 phpmyadmin 安装到 /var/www/nginx-default/phpMyAdmin/phpmyadmin
    • 这个答案对我帮助很大,非常感谢)我应该提到应该有';'在这一行的末尾:root /var/www/nginx-default/phpMyAdmin
    【解决方案3】:

    我为此苦苦挣扎了好几个小时,上面讨论的解决方案都不适用于我的情况(因为我需要运行 index.php、带参数的 index.php 以及 index.php 以外的其他 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;
        }
    

    【讨论】:

      【解决方案4】:

      也许它会搜索 index.html?尝试更改为

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

      并在下面添加部分,以避免与案例相关的问题

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

      【讨论】:

        【解决方案5】:

        这是一个为两个不同文件夹提供服务的配置的实际示例,一个受保护,另一个不受保护。

        Nginx 服务于两个文件夹: /usr/share/nginx/html/ 在 http:yourhost:port/ (受 htpasswd 保护) /usr/share/nginx/static/ 在 http:yourhost:port/health/ (打开以允许健康监控)

            location / {
                root   /usr/share/nginx/html/;
                index  index.html index.htm;
                auth_basic           "Administrator’s Area";
                auth_basic_user_file /etc/nginx/.htpasswd;
            }
        
            location ^~ /health/ {
                root   /usr/share/nginx/static;
                index  index.html index.htm;
            }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-05
          • 1970-01-01
          • 2010-11-26
          • 1970-01-01
          • 2013-05-16
          • 2020-11-07
          • 2012-07-30
          • 2018-03-29
          相关资源
          最近更新 更多