Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've already tried so much stuff in order to do this, also, by putting a php file inside /images/test.php to test if it will be downloaded or executed, and it always gets executed.

Also, I have lots of seo rewrites, so I don't really know what is happening.

This is what I am currently trying (and I already red lots of docs about it):

Server Config:

user nginx nginx;
worker_processes 1;

error_log /var/log/nginx/error_log info;

events {
    worker_connections 1024;
    use epoll;
}

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

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

    client_header_timeout 10m;
    client_body_timeout 10m;
    send_timeout 10m;

    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 2k;
    request_pool_size 4k;

    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 8k;
    gzip_types text/plain;

    output_buffers 1 32k;
    postpone_output 1460;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 75 20;

    ignore_invalid_headers on;

    index index.php;
    proxy_buffering on;
    proxy_buffer_size 8k;
    proxy_buffers 2048 8k;

    server {
        listen 127.0.0.1;
        server_name <<my_domain>>;

        access_log /var/log/nginx/access_<<my_domain>> main;
        error_log /var/log/nginx/error_<<my_domain>> info;

        root <<doc_root>>;

        error_page 403 /errors.php?type=403;
        error_page 404 /errors.php?type=404;

        location ~* /images  {
            root    <<doc_root>>;
            #expires 30d;
        }

        location ~ \.php$ {
            #internal;
            try_files $uri =404;
            expires off;
            include /etc/nginx/fastcgi.conf;
            fastcgi_pass unix:/var/run/php-fpm.socket;
        }

        #For virtual folders which are links seo-friendly and language
                location ~ (*UTF8)^.*$ {
                        include "rewrites.conf";
                }

    }

    server {
        listen 127.0.0.1:443;
        server_name <<my_domain>>;

        ssl on;
        ssl_certificate /etc/ssl/nginx/.crt;
        ssl_certificate_key /etc/ssl/nginx/.key;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA384:DHE-RSA-AES256-SHA256:RC4-SHA;
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1 SSLv3;
        ssl_prefer_server_ciphers on;

        access_log /var/log/nginx/access_ssl_<<my_domain>> main;
        error_log /var/log/nginx/error_ssl_<<my_domain>> info;

        root <<doc_root>>;
        error_page 403 /errors.php?type=403;
        error_page 404 /errors.php?type=404;

        <<the_same_locations_than_www_server_before>>
    }
}

So, can I serve all content inside /images/ statically without passing through php? In this testcase, the file /images/test.php should be downloaded rather than showing phpinfo();

Thanks

share|improve this question
    
After rebooting everything, it seems to work, but, any advise on this prior to mark it as resolved? –  StormByte Feb 5 at 17:23

2 Answers 2

up vote 1 down vote accepted

Because ~* matches but continues searching for other regex matches I think what you want is:

    location ^~ /images/  {
        #This should match /images/ and stop checking.
        root    <<doc_root>>;
        #expires 30d;
    }

    location ~ \.php$ {
        #internal;
        try_files $uri =404;
        expires off;
        include /etc/nginx/fastcgi.conf;
        fastcgi_pass unix:/var/run/php-fpm.socket;
    }
share|improve this answer
    
Thank you, I missed that "stop searching" :) –  StormByte Feb 6 at 8:29
    
And what about putting more than one folder in the same location rule? Something like ^~ /(images|js|css)/ do not work _:( –  StormByte Feb 7 at 8:37
    
This took some digging but it looks like you can't use a regex with ^~ because it stops as soon as it matches. I'm not explaining it well but the short answer is you can't. See configuration D here –  meatflag Feb 7 at 17:46
    
Thanks :) Sometimes nginx configuration is a little tricky. Anyway I added the rest of static folders as intependent locations using the syntax you provided –  StormByte Feb 7 at 17:54

Based on your location blocks, files ending with '.php' will only be passed through PHP. So if you have an actual file in the '/images' directory, it should serve it as a static file, or otherwise return a 404 error. If the file ends with '.php' and is in the '/images' directory, it will run through both location blocks in order.

Should it do otherwise?

share|improve this answer
    
Yes, I wanted to serve it statically, regardless of content, if it is inside /images/ folder. –  StormByte Feb 6 at 8:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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