开发者社区> 问答> 正文

为Apache配置nginx前端da版

下面是少量缓存 反代。配置方法,我运用在da下面。其他环境也适用, 只不要过改apache端口的时候修改对应文件即可

下载nginx安装包:

CentOS5:
wget http://download.efsync.org/Nginx/nginx-release-rhel-5-0.el5.ngx.noarch.rpm

CentOS6:
wget http://download.efsync.org/Nginx/nginx-release-rhel-6-0.el6.ngx.noarch.rpm
安装nginx:

CentOS5:
rpm -ivh nginx-release-rhel-5-0.el5.ngx.noarch.rpm

CentOS6:
rpm -ivh nginx-release-rhel-6-0.el6.ngx.noarch.rpm

安装完成后把nginx升级为最新版1.4
yum install -y nginx

添加一个用户和用户组用来运行nginx
groupadd www

useradd -s /sbin/nologin -g www www

修改nginx配置文件
先删除原配置文件
rm -rf /etc/nginx/nginx.conf
新建一个配置文件
vi /etc/nginx/nginx.conf

写入以下代码:(把里的IP改你的真实IP)
user www;
worker_processes 2;  #进程数
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";
        client_body_buffer_size  512k;
        proxy_connect_timeout    5;
        proxy_read_timeout       60;
        proxy_send_timeout       20;
        proxy_buffer_size        32k;
        proxy_buffers            4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;
        proxy_temp_path   /home/cache/temp;
        proxy_cache_path  /home/cache/path levels=1:2 keys_zone=cache_one:50m inactive=7d max_size=30g;
        limit_conn_zone $binary_remote_addr zone=default:3m;

server
      {
        listen          80;
        server_name     localhost;

        set $no_cache 0;
    if ($request_method = POST) {
        set $no_cache 1;
    }  

    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9] |wp-postpass|wordpress_no_cache|wordpress_logged_in|uin") {
        set $no_cache 1;
    }

        location /
               {
                limit_conn default 10;  #单ip进程数3
                proxy_cache_bypass $no_cache;
                proxy_no_cache $no_cache;
                proxy_cache cache_one;
                proxy_cache_valid  200 304 2s;   #各种状态缓存时间
                proxy_cache_valid  302 30m;
                proxy_cache_valid  301 2d;
                proxy_cache_valid  502 20s;
                proxy_cache_valid  any 10s;
                proxy_cache_key $host$uri$is_args$args;
                proxy_cache_use_stale invalid_header error timeout http_502 http_503 http_504;    #502 503 504使用旧缓存
                proxy_pass          http://公网ip:800;
                proxy_redirect      off;
                proxy_set_header    X-Real-IP       $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                expires 30s;
                access_log  off;
               }                

         location ~*\.(js|css|gif|png|bmp|jpeg|jpg|swf)$
               {
                proxy_pass http://公网ip:800;
                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_cache cache_one;
                proxy_cache_valid  200 304 3m;
                proxy_cache_key $host$uri$is_args$args;
                expires 1d;
               }
         location ~* wp\-.*\.php|wp\-admin {
                limit_conn default 1;
                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_cache_lock on;
                proxy_cache cache_one;
                proxy_cache_valid  any 3m;
                proxy_cache_key $host$uri$is_args$args;
                proxy_pass http://公网ip:800;
                expires 1h;
              }
        }
}


修改Apache的端口为800
sed -i "s#ips.conf#ips_hack.conf#g" /etc/httpd/conf/extra/httpd-vhosts.conf
cp /etc/httpd/conf/ips.conf /etc/httpd/conf/ips_hack.conf
sed -i "s#:80#:800#g" /etc/httpd/conf/ips_hack.conf
sed -i "s#:80#:800#g" /etc/httpd/conf/extra/httpd-vhosts.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/ips_virtual_host.conf
sed -i "s#:|PORT_80|#:800#g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s#:|PORT_80|#:800#g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s#:|PORT_80|#:800#g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s#:|PORT_80|#:800#g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s#:|PORT_80|#:800#g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s#Port 80#Port 800#g" /usr/local/directadmin/data/templates/httpd.conf
sed -i "s#Listen 80#Listen 800#g" /usr/local/directadmin/data/templates/httpd.conf
sed -i "s#Listen 80#Listen 800#g" /etc/httpd/conf/httpd.conf
sed -i "s#:80#:800#g" /usr/local/directadmin/data/users/*/httpd.conf



重启apache
service httpd restart

重启nginx
/etc/init.d/nginx start
大功告成

展开
收起
holinhot 2013-08-31 14:30:49 12312 0
2 条回答
写回答
取消 提交回答
  • 谢谢分享
    2013-08-31 14:47:44
    赞同 展开评论 打赏
  • Re为Apache配置nginx前端da版
    提示一下这个方法会造成无法统计来仿者ip具体解决方法请看另一帖子
    关于上传文件大小设置的问题你在php.ini设置了大小后还需要到nginx设置post大小不然大文件一样传不上去。请在nginx.conf文件的http区域加入
    client_max_body_size 500M;
    这个500M代表你设置的文件大小
    2013-08-31 14:33:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Vue.js 在前端服务化上的探索与实践 立即下载
阿里文娱大前端技术实践 立即下载
前端代码是怎样智能生成的 立即下载

相关镜像