Nginx location directive doesn't seem to be working. Am I missing something?

后端 未结 4 764
逝去的感伤
逝去的感伤 2020-12-07 09:59

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 php

相关标签:
4条回答
  • 2020-12-07 10:22

    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)
    

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 10:25

    Set the 'root' directly. Less directives, less computing needed to set more vars. There are also other things (like fastcgi_param DOCUMENT_ROOT) that will not be set correctly in the currently accepted answer. This method will handle all that however:

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 10:31

    Perhaps it searches for index.html? Try changing to

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

    and add section below, to avoid case-related issues

    location /phpMyAdmin {
       rewrite ^/* /phpmyadmin last;
    }
    
    0 讨论(0)
  • 2020-12-07 10:46

    I struggled with this for hours now and none of the solutions discussed above worked to my case (since i needed to run index.php, index.php with arguments, and other php scripts than index.php), but finally reached the working configurations as the following:

        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;
        }
    
    0 讨论(0)
提交回复
热议问题