Sputnik
Sputnik

Reputation: 53

Virtual directory running PHP in nginx

Is there a proper way of specifying what backend contents of a virtual directory should be sent to in nginx? Currently, I have a site running nginx with tomcat and PHP-FPM where requests are being sent to tomcat as default, I want to add a virtual directory called dbadmin (contents of this directory are stored on completely different place on disk) containing phpmyadmin where requests are sent to PHP-FPM socket.

My current config (conf.d/default.conf):

server {
  listen       80 default_server;
  server_name  mydomain.com;
  root         /opt/apache-tomcat/webapps;

  # cache statis files for 1 month
  location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           30d;
  }

  # deny access to hidden files (.whatever)
  location ~ /\. {
    access_log off;
    log_not_found off;
    deny all;
  }

  location ~ /dbadmin/(.*)$
  {
    index index.php;
    alias /opt/www/phpmyadmin/$1;
  }

  location ~* \.php$ {
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    /opt/www/phpmyadmin$fastcgi_script_name;
    fastcgi_index  index.php;

    if (-f $request_filename) {
      fastcgi_pass phpfpm;
    }
  }

  location / {
    # increase timeout to 2 min
    proxy_read_timeout        120;

    # needed to forward user IP address
    proxy_set_header          X-Real-IP $remote_addr;

    # https support
    proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header          Host $http_host;

    proxy_redirect            off;
    proxy_pass                http://127.0.0.1:8080;
  }
}

phpfpm is defined in main nginx.conf:

upstream phpfpm {
  ip_hash;
  server unix:/var/run/php-fpm/php-fpm.sock;
}

Upvotes: 1

Views: 924

Answers (1)

Alberto S.
Alberto S.

Reputation: 2115

Reading your code, I think solution could be found in the way you send the .php files to php-fpm, usually you should try something like this....

location ~* \.php$ {    
fastcgi_pass  127.0.0.1:9000;          # if using tcp/ip
fastcgi_pass unix:/tmp/php5-fpm.sock;  # if using sockets
}

Upvotes: 1

Related Questions