详解nginx反向代理配置及优化

2019-10-17 19:07:53刘景俊

#参数都有所调整.目的是解决代理过程中出现的一些502 499错误   

#Add expires header for static content
   location ~* .(jpg|jpeg|gif|png|swf)$ {
     if (-f $request_filename) {
       root /date/wwwroot/linuxtone/;
       expires      1d;
       break;
      }
   }

     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" $http_x_forwarded_for';
    access_log  /exp/nginxlogs/bbs.linuxtone_access.log  access;
  
}

注:第二种代理方式

nginx 处理下图片,html等静态的东西.其它动态由apache处理.因此apache也需要做一些参数调整.

设置图片等过期时间.缓解请求.

如果源与nginx在同一台机器建议使用如下方法:

location / {
      proxy_pass  http://192.168.1.4:8099/;
      proxy_redirect default ;
      }

针对不同的目录进行代理把下面的配置放到根目录代理的上面

        location /linuxtone/ {
              proxy_pass  http://192.168.1.4:8099/linuxtone/;
              proxy_redirect default ;
         }

4 源配置

<VirtualHost 192.168.1.4:8099>
    ServerAdmin liuyu105#gmail.com
    DocumentRoot /date/wwwroot/linuxtone
    ServerName bbs.linuxtone.com
    ErrorLog logs/linuxtone_error_log
   CustomLog "|/usr/local/sbin/cronolog logs/linuxtone_access_log.%Y%m%d" combined
</VirtualHost>

 第3部分:源的优化

1 apache-mpm.conf

<IfModule mpm_prefork_module>
    StartServers          15
    MinSpareServers       15
    MaxSpareServers      30
    ServerLimit         2536
    MaxClients          2048
    MaxRequestsPerChild   1500
</IfModule>

2 apache-keepalive

Timeout 120  #与nginx的保持一至
KeepAlive On
MaxKeepAliveRequests 400
KeepAliveTimeout 7

第4部分:PHP的优化

优化一:将PHP由之前的xcache换成eaccelerator

1 安装

wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php5/bin/php-config
make
make install

注:PHP路径以安装为准!

2 配置

sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/"nextension = "memcache.so"n#' /etc/php.ini
sed -i 's#output_buffering = Off#output_buffering = On#' /etc/php.ini
sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /etc/php.ini

配置eAccelerator加速PHP: