在AWS Elastic Beanstalk中配置nginx缓存

时间:2016-11-17 19:03:28

标签: amazon-web-services nginx elastic-beanstalk

我需要在EB中更改我的nginx反向代理的配置。在我的本地环境中,我已经配置完好并正常工作,但是当我尝试更改 proxy_cache_path 和其他内容时,它无法正常工作。

这是我的本地配置( nginx.conf ),这里重要的是proxy_cache_path和配置缓存部分:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    proxy_cache_path /cache/nginx levels=1:2 keys_zone=cache_zone_name:10m;

    server {
        listen       80;
        server_name  mydomain.app, www.mydomain.app;



        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;

            #Config proxy inverse cache
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;

            # Add cache debugging header
            add_header X-Cache-Status $upstream_cache_status;

            # Configure cache
            proxy_cache        cache_zone_name;
            proxy_cache_valid  any 1m;
            proxy_cache_key    $scheme$host$request_uri;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    include servers/*;
}

我从官方文档中获取并与我的实例进行比较 .ebextensions / proxy.config

files:
  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:8081;
        keepalive 256;
      }

      server {
        listen 8080;
        # proxy_cache_path /home levels=1:2 keys_zone=cache_zone_name:10m;
        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
            set $year $1;
            set $month $2;
            set $day $3;
            set $hour $4;
        }
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  main;

        location / {
            proxy_pass  http://nodejs;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

            # Add cache debugging header
            add_header X-Cache-Status $upstream_cache_status;

            # Configure cache
            # proxy_cache        cache_zone_name;
            # proxy_cache_valid  any 1m;
            # proxy_cache_key    $scheme$host$request_uri;
        }

        gzip on;
        gzip_comp_level 4;
        gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location /public {
            alias /var/app/current/public;
        }

      }

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

我已将文件夹 / home 配置为缓存,如果我取消注释proxy_cache_path和Configure Cache部分的行,则部署失败。

有什么想法吗?我花了2个多小时没有结果......谢谢!!

1 个答案:

答案 0 :(得分:1)

好的,我解决了将线路移出服务器的问题:

 proxy_cache_path /home levels=1:2 keys_zone=cache_zone_name:10m;

 server { ... other code }

我希望有人帮我解决问题。谢谢!