Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. Http://liuyu.blog.51cto.com/183345/166381nginx reverse proxy configuration and optimization
2009-05-26
Author: Hold Every day
Blog:liuyu.blog.51cto.com
bbs:bbs.linuxtone.org
Msn:liuyubj520#hotmail.com
Email:liuyu105#gmail.com
Objective:
Because the server Apache is unable to withstand the current concurrency. Plus the front squid configuration, The problem remains unresolved. Most of the page programs are dynamic. You cannot use fastcgi to handle it. So you want to use Nginx as the reverse proxy Apache. The entire configuration installation process is simple. In case of high concurrency, Some optimizations have been made before the installation. The current configuration can withstand more than 3000 concurrency. It's not as big as it seems. But there are only a few 499 questions. Expecting someone to talk to me about the solution.
Part 1th: Installation
1 Setting up Users and Groups
/usr/sbin/groupadd www
/usr/sbin/useradd-g www www
2 installation pcre let Nginx support rewrite convenient for later required
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
3 Installing 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
#注意上文中的--with-cc-opt= '-o2 '--with-cpu-opt=opteron This is the compiler optimizer, the most commonly used now is 02 instead of 3. The corresponding model of the CPU, can refer to: http://wiki.gentoo.tw/ Index.php/howto_cflag
Part 2nd: Configuration and optimization of configuration files
1 nginx.conf configuration file:
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 the can is 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;
#参数都有所调整. To resolve some 502 499 errors that occur during the agent process
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 120; #参数加大 to resolve 502 error when acting as Agent
Tcp_nodelay on;
Include vhosts/upstream.conf;
Include vhosts/bbs.linuxtone.conf;
}
2 upstream.conf configuration file (This is also the way to do load configuration)
Upstream.conf
Upstream bbs.linuxtone.com {
Server 192.168.1.4:8099;
}
3 Site configuration file
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;
}
#参数都有所调整. To resolve some 502 499 errors that occur during the agent 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 type of proxy
Nginx processing tablets, HTML and other static things. Other dynamics are handled by Apache. So Apache also needs to make some parameter adjustments.
Set an expiration time such as a picture. Mitigation requests.
If the source and Nginx in the same machine is recommended to use the following methods:
Location/{
Proxy_pass http://192.168.1.4:8099/;
Proxy_redirect default;
}
Agent for different directories put the following configuration on top of the root directory agent
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 3rd: Optimization of the source
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 #与nginx的保持一至
KeepAlive on
Maxkeepaliverequests 400
KeepAliveTimeout 7
Part 4: Optimization of PHP
Optimization One: Change PHP from previous XCache to Eaccelerator
1 installation
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
Note: The PHP path 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
Configuring Eaccelerator Acceleration PHP:
Mkdir-p/usr/local/webserver/eaccelerator_cache
Vi/etc/php.ini
Press Shift+g to jump to the end of the configuration file, plus the following configuration information:
[Eaccelerator]
zend_extension= "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
Eaccelerator.shm_size= "128"
Eaccelerator.cache_dir= "/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "300"
Eaccelerator.shm_prune_period= "120"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"
Optimization Two: Contact development recompile PHP to reduce the number of PHP modules. This piece, although it has little impact, has a certain effect. You can also use the Nginx compiler to install it before compiling.
Part 5th: Test and start Nginx
Ulimit-shn 51200
/usr/local/webserver/nginx/sbin/nginx-t
/usr/local/webserver/nginx/sbin/nginx
Part 6th: Nginx Log Cutting script
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
Logs_path= "/exp/nginxlogs/"
Mkdir-p ${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/
MV ${logs_path}bbs.linuxtone_access.log ${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/ bbs.linuxtone_access_$ (date-d "Yesterday" + "%y%m%d"). Log
KILL-USR1 ' Cat/usr/local/webserver/nginx/nginx.pid '
Crontab-e
XX * * * */bin/bash/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
This article is from the "Seven" blog, make sure to keep this source http://liuyu.blog.51cto.com/183345/166381
Nginx reverse proxy configuration and optimization