0

This current configuration is so close to working. The only issue is, it is serving my php files as static files (instead of handing them off to apache2, running on port 81)

I know it is something wrong with this line here location ~ .+(?<!\.php). Do I have the negative lookahead syntax wrong?

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com;
    index index.php;

    server_name example.com;

    location ~ .+(?<!\.php) {
        try_files $uri $uri @php;
    }

    location @php {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:81;
        proxy_redirect http://127.0.0.1:81/ /;
        proxy_read_timeout 60s;
    }

    location ~ /\.ht {
        deny all;
    }
}

3 Answers 3

0

I usually...

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

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass_header Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    fastcgi_pass your_proxy;
    fastcgi_index index.php;
    include fastcgi.conf;
    add_header X-Proxy-Cache-PHP $upstream_cache_status;
}
0

Since you know you want to serve all PHP files via Apache, you consider amending this line:

location ~ .+(?<!\.php) {
    try_files $uri $uri @php;
}

to:

location ~ .+(?<!\.php) {
    try_files @php $uri $uri;
}

Or, much simpler, change your server root, to ensure it will not find your PHP files locally:

root /dev/null;

Basically, your problem is that nginx can in fact find the files it is looking for, so try_files is returning the raw PHP. Unless you specifically want/need to serve static assets, just pass everything on to your backend, rather than looking for a local copy of the file.

0

Here is what I have done:

apache2.conf

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_pass http://127.0.0.1:81;
proxy_redirect http://127.0.0.1:81/ /;
proxy_read_timeout 60s;

main.conf

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com;

    server_name example.com;

    location ~ \.php {
        include apache2.conf;
    }

    location / {
        try_files $uri @php;
    }

    location @php {
        include apache2.conf;
    }

    location ~ /\.ht {
        deny all;
    }
}

Open for improvement, but I think this is the most concise configuration I could write.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .