To configure Nginx to process a PHP app, one can create a dedicated conf file under sites-available and then create an according symlink under sites-enabled, but I don't want to use this method - I prefer one global conf for running php scripts, for all projects. Something that could be putted inside nginx.conf. I didn't find such a global conf anywhere.

Is there a Nginx global conf for running php scripts?


Something like this. You just need to keep you project in a folder accoding to it's domain name ($host) Check nginx variables: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_host

user  www-data;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  8192;

http {
    server_tokens off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    tcp_nodelay     on;

    keepalive_timeout  65;
    #gzip  on;

    server {
    listen 80 default_server;

    root /var/www/$host;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;

    location ~ /\.          { access_log off; log_not_found off; deny all; }

    rewrite /files/$ /index.php last;

    if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;

    location ~ \.php$ {
        fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
        client_max_body_size 25M;
        try_files      $uri =404;
        fastcgi_index  index.php;
        include        /etc/nginx/fastcgi_params;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.