1

I search on google and a lot of other questions here, but none helped me with my problem.

So what I'm trying to do is to serve all statics contents from s3 and the only ones that has to run in server-side with Apache on EC2.

Below Is my nginx configuration:

# WP Bullet nginx proxy cache
# Author Mike from https://guides.wp-bullet.com
#fix 504 gateway timeouts, can go in nginx.conf
proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;
#set the location of the cached files, zone, name, size (1000 MB) and how long to cache for 600 minutes
proxy_cache_path  /var/run/proxy_cache levels=1:2 keys_zone=WORDPRESS-PROXY:10m max_size=1000m inactive=600m use_temp_path=off;
proxy_cache_key $scheme$host$request_uri;
#prevent header too large errors
proxy_buffers 256 16k;
proxy_buffer_size 32k;
#httpoxy exploit protection
proxy_set_header Proxy "";
# add forwarded for header
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server {
listen          80 default;
access_log /var/log/nginx/proxy-access.log;
error_log /var/log/nginx/proxy-error.log;
# show cache status and any skip cache reason
add_header X-Proxy-Cache $upstream_cache_status;
add_header Cache-BYPASS-Reason $skip_reason;

# define nginx variables
set $do_not_cache 0;
set $skip_reason "";
set $bypass 0;

# security for bypass so localhost can empty cache
if ($remote_addr ~ "^(127.0.0.1|Web.Server.IP)$") {
    set $bypass $http_secret_header;
}

# skip caching WordPress cookies
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
    set $do_not_cache 1;
    set $skip_reason Cookie; 
}

# Don't cache URIs containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
    set $skip_cache 1;
    set $skip_reason URI; 
}

location / {
    proxy_set_header Host $host;
    # may need to comment out proxy_redirect if get login redirect loop
    proxy_redirect off;
    proxy_cache WORDPRESS-PROXY;
    proxy_cache_revalidate on;
    proxy_ignore_headers  Expires Cache-Control;
    proxy_cache_use_stale  error timeout invalid_header updating http_500 http_502 http_503 http_504;
    proxy_cache_bypass $bypass $do_not_cache;
    proxy_no_cache $do_not_cache;
    proxy_cache_valid 200 301 302 500m;
    proxy_cache_valid 404 1m;
    #can rename PURGE to whatever you want, should restrict it to backend server requests for security
    proxy_cache_purge PURGE from 127.0.0.1 Web.Server.IP;
    # pass requests onto your PHP backend
    proxy_pass  http://127.0.0.1:8080;
    }

# allows purging via special URL
location ~ /purge(/.*) {
    allow 127.0.0.1;
    allow Web.Server.IP;
    deny all;
    proxy_cache_purge WORDPRESS-PROXY $scheme$host$1;
    }
}

But I really don't know how to identify the requests ending with .php to proxy to Apache on EC2, and send all others one to S3. But there is another problem, I have multiples domains pointing to that server, and multiples folder on EC2.

The static content shares the same files on S3, but not the requests that has /upload/ in path. This ones is proxied to another folder on s3.

This is the structure:

Domain / Folder in EC2 site1.com / site1 site2.com.br / site2 site3.ar / site3

Ends in PHP:

Nginx > send request to EC2. Apache will handle the folder that answers to this domain.

Static but path don't have /uploads/

Nginx > send request to S3 a bucket to a shared folder named static, but it keeps the path structure.

Static but has /uploads/

Nginx > send request to S3, but the path is files/site1/file_name.(jpg/png/mp4/all others). Other words, don't keep path structure.

Can anyone help me doing that. I new at nginx, and I not seeing a way that a could do that.

Your Answer

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

Browse other questions tagged or ask your own question.