Detail nginx reverse agent configuration and optimization

  • 2020-05-12 06:58:21
  • OfStack

Preface:

Due to server apache resist the concurrent. Plus the front-end squid configuration, still can solve problem. And the most dynamic page program. Can't use fastcgi to deal with. So want to use nginx as a reverse proxy apache. The entire configuration installation process is simple. In the case of considering the high concurrency, do some optimization before installation. The current configuration can withstand more than 3000 concurrent. As if is not particularly big & # 63; Ha ~~ but enough ~~ there are only a few problems with 499.. I am looking forward to someone to discuss it with me

Part 1: installation

Create users and groups


/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

2 install pcre to make nginx support rewrite convenient for later use


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install

The installation nginx


wget http://sysoev.ru/nginx/nginx-0.7.58.tar.gz
tar zxvf nginx-0.7.58.tar.gz
cd nginx-0.7.58/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2' --with-cpu-opt=opteron
make && make install

# note of the above - with - cc - opt = '- O2' - with - cpu - opt = opteron this is compiler optimizations, is currently the most commonly used - 02 instead of 3. The corresponding CPU model, can be reference: http: / / wiki gentoo. tw/index php/HOWTO_CFLAG

Part 2: configuring and optimizing configuration files

1 nginx.conf profile:


user  www www;
worker_processes 4;

# [ debug | info | notice | warn | error | crit ]
error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;
pid        /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
     use epoll;
     worker_connections 51200;
}

http
{
     include       mime.types;
     default_type  application/octet-stream;
     source_charset GB2312;
     server_names_hash_bucket_size 256;
     client_header_buffer_size 256k;
     large_client_header_buffers 4 256k;

     #size limits
     client_max_body_size       50m;
     client_body_buffer_size    256k;
     client_header_timeout   3m;
     client_body_timeout 3m;
     send_timeout       3m;
# The parameters are adjusted . The purpose is to resolve issues that arise during the proxy process 1 some 502 499 error    
     sendfile on;
     tcp_nopush     on;
     keepalive_timeout 120; # Parameters increase , To solve when acting as an agent 502 error 
     tcp_nodelay on;
    
     include          vhosts/upstream.conf;
     include          vhosts/bbs.linuxtone.conf; 

}

2 upstream.conf configuration file (this is also how you configure the load)


upstream.conf
      upstream bbs.linuxtone.com {
         server 192.168.1.4:8099;
       }

3. Site profile


bbs.linuxtone.conf
server
   {
      listen       80;
      server_name  bbs.linuxtone.conf;
      charset GB2312;
      index index.html index.htm;
      root  /date/wwwroot/linuxtone/;

        location ~ ^/NginxStatus/ {
            stub_status on;
            access_log off;
         }

     location / {
       root  /date/wwwroot/linuxtone/;
       proxy_redirect off ;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header REMOTE-HOST $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       client_max_body_size 50m;
       client_body_buffer_size 256k;
       proxy_connect_timeout 30;
       proxy_send_timeout 30;
       proxy_read_timeout 60;
       proxy_buffer_size 256k;
       proxy_buffers 4 256k;
       proxy_busy_buffers_size 256k;
       proxy_temp_file_write_size 256k;
       proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
       proxy_max_temp_file_size 128m;
       proxy_pass  http://bbs.linuxtone.com;
      }

The # parameters have been adjusted to resolve 1 502 499 errors in the proxy process


#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;
  
}

Note: the second way of agency

nginx handles pictures,html and other static things. Other dynamic things are handled by apache. Therefore, apache also needs to make some parameter adjustment.

Set image and other expiration time. Alleviate the request.

If the source is on the same machine as nginx, the following method is recommended:


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

Proxy for different directories put the following configuration on top of the root directory proxy


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

4 source configuration


<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>

Part 3: source optimization

1 apache-mpm.conf


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
0

2 apache-keepalive


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
1

Part 4: optimization of PHP

Optimization 1: replace PHP with eaccelerator instead of xcache

1 installation


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
2

Note: the path of PHP is subject to installation!

2 configuration


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

Configure eAccelerator acceleration PHP:


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
4

Press shift+g to jump to the end of the configuration file, and add the following configuration information:


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
5

Optimization 2: contact development to recompile php to reduce the php module, so as to reduce the amount of memory consumed by the php process.

Part 5: test and launch nginx


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
6

Part 6 :nginx log cutting script


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
7

crontab -e

00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh


Related articles: