La direttiva sulla posizione di Nginx non sembra funzionare. Mi sto perdendo qualcosa?
-
06-07-2019 - |
Domanda
Ho impostato Nginx come mio web server principale e ho due server basati su Mochiweb. Alcune richieste sono inviate in proxy inverso a questi due server. ora voglio accedere a phpmyadmin (che si trova in / var / www / nginx-default / phpMyAdmin) usando nginx, ma continua a dire che l'errore 404 non è stato trovato. Mi sto perdendo qualcosa di ovvio qui?
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;
}
}
Soluzione
Il problema qui è che solo il "migliore" La direttiva location
viene presa, in questo ordine:
location = <path> (longest match wins)
location ^~ <path> (longest match wins)
location ~ <path> (first defined match wins)
location <path> (longest match wins)
Usando questo set di regole, la tua direttiva / phpmyadmin
location
è battuta dall'espressione regolare " .php $
" direttiva location
, quindi la prima viene completamente ignorata. Inoltre, la direttiva php fastcgi è cablata nella directory / home / me / dev
, il che significa che phpMyAdmin è totalmente inaccessibile. Puoi usare una riscrittura per ottenere la radice corretta per i tuoi script 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;
}
Altri suggerimenti
Imposta direttamente la 'radice'. Sono necessarie meno direttive, meno elaborazione per impostare più errori. Ci sono anche altre cose (come fastcgi_param DOCUMENT_ROOT) che non verranno impostate correttamente nella risposta attualmente accettata. Questo metodo gestirà comunque tutto ciò:
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;
}
Ho lottato con questo per ore ormai e nessuna delle soluzioni discusse sopra ha funzionato nel mio caso (poiché avevo bisogno di eseguire index.php, index.php con argomenti e altri script php di index.php), ma alla fine ho raggiunto le configurazioni di lavoro come le seguenti:
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;
}
Forse cerca index.html? Prova a cambiare in
location /phpmyadmin {
root /var/www/nginx-default/phpMyAdmin;
index index.php;
}
e aggiungi la sezione seguente, per evitare problemi relativi al caso
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}