Use Nginx to replace apache for a high-performance Web environment

Source: Internet
Author: User
Author: NetSeek welcome reprint, reprint please note the source: bbs.linuxpk.com original link: bbs.linuxpk.comthread-11845-1-1.html Nginx introduction: Nginx pronunciation for [enginex], is a project established by Russian IgorSysoev, based on BSD license. It is said that he was originally a member of F5. homepage: http author: NetSeek
Welcome to reprint, reprint please indicate the source: http://bbs.linuxpk.com
Link: http://bbs.linuxpk.com/thread-11845-1-1.html

Nginx introduction:
Nginx is pronounced as [engine x] and is a project established by the Russian Igor Sysoev based on the BSD license.
It is said that he was a member of F5, home: http://nginx.net. Some of Russia's large websites have been using it for more than two years and have always performed well, I believe all of my friends who want to know about nginx have read the article about using nginx to achieve server load balancer (6 ).

Test Environment: redocn provides an operating server environment.

About the Red moving service environment:
Hongdong China's early use of the apache environment, coupled with some optimization work, has been relatively stable, but recently due to the development of the website, the access volume is growing, the number of online users often appear, the load is too high, the performance drops sharply. with the consent of the webmasters of Shuangmu, it is suggested that nginx can be used to replace apache. after a long period of observation, nginx is stable and the system will not talk about the current high load, memory usage is also very low, and the access rate is significantly improved from the user experience.

About China:
After nearly one year of rapid development, the redocn Forum currently has an average of over 1 million page views per day, ranking 1st in the global design Forum (Chinese, is one of the most influential design forums in China. At present, the forum has nearly 0.2 million members, including the backbone of many leading figures in the design field, teachers and students of relevant art schools, and some design lovers.

Migration goal: to achieve static website forum, anti-leeching, download concurrency and speed limit, to achieve all the functions of apache on the original site, and migrate all the sites in the original apache environment to Nginx

I. php (Fastcgi) compilation and installation
[Root @ att php-5.2.4] # cat in. sh./configure \

-PRefix =/usr/local/php-fcgi \

-Enable-fastcgi \

-Enable-discard-path \

-Enable-force-cgi-redirect \

-With-config-file-path =/usr/local/php-fcgi/etc \

-Enable-zend-multibyte \

-With-MySQL =/usr/local/mysql \

-With-libxml-dir =/usr/local/libxml2 \

-With-gd =/usr/local/gd2 \

-With-jpeg-dir \

-With-png-dir \

With-bz2 \

-With-freetype-dir \

-With-iconv-dir \

-With-zlib-dir \

-With-openssl =/usr/local/openssl \

-With-mcrypt =/usr/local/libmcrypt \

-Enable-sysvsem \

-Enable-inline-optimization \

-Enable-soap \

-Enable-gd-native-ttf \

-Enable-ftp \

-Enable-mbstring \

-Enable-exif \

-Disable-debug \

Disable-ipv6

Make

Make install

Cp php. ini-dist/usr/local/php-fcgi/etc/php. ini
Note: This article will not introduce how to install the gd library and mysql compilation and installation. the focus of this article is to introduce nginx installation and configuration. if you want to know other related problems, you can go
LinuxPk to find the relevant posts (http://bbs.linuxpk.com)

II. Nginx compilation and installation
1. create the nginx running user and virtual host directory groupadd www-g 48

Useradd-u 48-g www

Mkdir-p/data/www/wwwroot

Chown-R www: www/data/www/wwwroot
2. install the spawn-fcgi included in lighttpd to start php-cgi.
First compile and install lighttpd to generate the spawn-fcgi binary file. cd/usr/local/src/lighttpd-1.4.18
Http://www.cncms.com/
Cp src/spawn-fcgi/usr/local/php-fcgi/bin/
Start the php-cgi process and listen to port 8085 of 127.0.0.1. the number of processes is 250 (if the server memory is smaller than 3 GB, only 25 processes can be started). the user is www:
/Usr/local/php-fcgi/bin/spawn-fcgi-a 127.0.0.1-p 8085-C 250-u www-f/usr/local/php-fcgi/bin/php -cgi

3. install and configure nginx
Install the pcre library required by Nginx:
Http://ftp.dk.debian.org/exim/pcre/pcre-7.3.tar.gz tar zxvf pcre-7.2.tar.gz

Cd pcre-7.2/

./Configure

Make & make install

Cd ../

Http://sysoev.ru/nginx/nginx-0.5.32.tar.gz

Tar zxvf nginx-0.5.32.tar.gz

Cd nginx-0.5.32

./Configure-user = www-group = www-prefix =/usr/local/nginx/-with-http_stub_status_module--

Openssl =/usr/local/openssl

Make & make install
This module is a non-core module that needs to be manually added to the compilation parameters during compilation-with-http_stub_status_module
Configure nginx

III. Nginx main configuration file and PHP support.

1. configure the main configuration file of nginx. conf
# Cd/usr/local/nginx/conf/
# Cp nginx. conf nginx. conf. cao
# Cat/dev/null> nginx. conf
# Vi nginx. conf // Main configuration file user www;

Worker_processes 10;

# Error_log logs/error. log;

# Error_log logs/error. log notice;

# Error_log logs/error. log info;

Pid/var/run/nginx. pid;

# Specifies the value for maximum file descriptors that can be opened by this process.

Worker_rlimit_nofile 51200;

Events

{

Use epoll;

# Maxclient = worker_processes * worker_connections/cpu_number

Worker_connections 51200;

}

Http

{

Include conf/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/data/www/logs/access. log main;

# Sendfile on;

Tcp_nopush on;

Tcp_nodelay off;

Keepalive_timeout 60;

 

Client_header_timeout 3 m;

Client_body_timeout 3 m;

Send_timeout 3 m;

Connection_pool_size 256;

Client_header_buffer_size 1 k;

Large_client_header_buffers 4 2 k;

Request_pool_size 4 k;

Output_buffers 4 32 k;

Post pone_output 1460;

Client_max_body_size 10 m;

Client_body_buffer_size 256 k;

Client_body_temp_path/dev/shm/client_body_temp;

Proxy_temp_path/usr/local/nginx/proxy_temp;

Fastcgi_temp_path/usr/local/nginx/fastcgi_temp;

 

# Gzip

Gzip on;

Gzip_http_version 1.0;

Gzip_comp_level 2;

Gzip_proxied any;

Gzip_types text/plain text/html text/CSS application/x-javascript text/xml application/xml + rss text/Javascript;

Gzip_min_length 1100;

Gzip_buffers 4 8 k;

# The following hosts des are specified for virtual hosts // The following describes how to load The virtual host configuration.

# Www.redocn.com

Include conf/vhosts/www_redocn_com.conf;

# Bbs.redocn.com

Include conf/vhosts/bbs_redocn_com.conf;

# Blog.redocn.com

Include conf/vhosts/blog_redocn_com.conf;

# Down.redocn.com

Include conf/vhosts/down_redocn_com.conf;

}
2. configure PHP that supports Fastcgi mode
[Root @ redocn conf] # cat enable_php5.conffastcgi_pass 127.0.0.1: 8085;

Fastcgi_index index. php;

Fastcgi_param GATEWAY_INTERFACE CGI/1.1;

Fastcgi_param SERVER_SOFTWARE nginx;

# New ac upload

# Fastcgi_pass_request_body off;

# Client_body_in_file_only clean;

# Fastcgi_param REQUEST_BODY_FILE $ request_body_file;

#

Fastcgi_param QUERY_STRING $ query_string;

Fastcgi_param REQUEST_METHOD $ request_method;

Fastcgi_param CONTENT_TYPE $ content_type;

Fastcgi_param CONTENT_LENGTH $ content_length;

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

Fastcgi_param SCRIPT_NAME $ fastcgi_script_name;

Fastcgi_param REQUEST_URI $ request_uri;

Fastcgi_param DOCUMENT_URI $ document_uri;

Fastcgi_param DOCUMENT_ROOT $ document_root;

Fastcgi_param SERVER_PROTOCOL $ server_protocol;

Fastcgi_param REMOTE_ADDR $ remote_addr;

Fastcgi_param REMOTE_PORT $ remote_port;

Fastcgi_param SERVER_ADDR $ server_addr;

Fastcgi_param SERVER_PORT $ server_port;

Fastcgi_param SERVER_NAME $ server_name;

# PHP only, required if PHP was built with-enable-force-cgi-redirect

Fastcgi_param REDIRECT_STATUS 200;
4. Multi-VM application configuration case.

# Mkdir/usr/local/nginx/conf/vhosts // Create a VM configuration storage directory.
1.www.redocn.com // Configure the first site
[Root @ redocn vhosts] # vi www_redocn_com.confserver

{

Listen 80;

Server_name www.redocn.com;

Index index.html index.htm index. php;

Root/data/www/wwwroot;

Error_page 404 http://bbs.redocn.com;

Rewrite ^/bbs/(. *) http://bbs.redocn.com/4101;

Location ~ . * \. Php? $

{

Include conf/enable_php5.conf;

}

}
Note: Regarding the rewite requirements, Hongdong China hopes that when users access http://www.redocn.com/bbs, they will automatically go to http://bbs.redocn.com
Use redirect in original apache
Redirect/bbs http://bbs.redocn.com

In this article, we use rewrite to implement nginx:
Rewrite ^/bbs/(. *) http://bbs.redocn.com/4101;
2. [root @ redocn vhosts] vi bbs_redocn_com.confserver

{

Listen 80;

Server_name bbs.redocn.com yan.redocn.com majia.redocn.com wt.redocn.com;

Index index.html index.htm index. php;

Root/home/www/htdocs/bbs;

Access_log/var/log/nginx/access_bbs.redocn.com.log combined;

Location /{

# Bbs rewrite

Rewrite ^/archiver/(fid | tid)-[\ w \-] + \. html) $/archiver/index. php? $1 last;

Rewrite ^/forum-([0-9] +)-([0-9] +) \. html $/forumdisplay. php? Fid = $1 & page = $2 last;

Rewrite ^/thread-([0-9] +)-([0-9] +)-([0-9] +) \. html $/viewthread. php? Tid = $1 & extra = page \ % 3D $3 & page = $2 last;

Rewrite ^/space-(username | uid)-(. +) \. html $/space. php? $1 = $2 last;

Rewrite ^/tag-(. +) \. html $/tag. php? Name = $1 last;

Break;

# Error

Error_page 404/index. php;

# Redirect server error pages to the static page/50x.html

Error_page 500 502 503 x.html;

Location =/50x.html {

Root html;

}

}

# Preventing hot linking of images and other file types

Location ~ * ^. + \. (Gif | jpg | png | swf | flv | rar | zip) $ {

Valid_referers none blocked server_names

* .Redocn.com redocn.com * .taobao.com taobao.com

Bbs.blueidea.com bbs.asiaci.com bbs.arting365.com forum.chinavisual.com softbbs.pconline.com.cn

Bbs.chinaddu.com bbs.photops.com * .baidu.com * .google.com * .google.cn * .soso.com * .yahoo.com.cn

* .Yahoo.cn;

If ($ invalid_referer ){

Rewrite ^/http://www.redocn.com/images/redocn.gif;

# Return 403;

}

}

 

# Support php

Location ~ . * \. Php? $

{

Include conf/enable_php5.conf;

}

 

}
Note:
1. Red moving China adopts high-performance Discuz! Forum, the original apache rewrite rules can be transplanted to nginx without any modification.
Static Configuration: # bbs rewrite section.
2. Generally, the anti-Leech function is easily implemented in apache in forums? Is it easy to implement in nginx? The answer is yes. # Preventing hot linking of images and other file types

Valid_referers none blocked server_names * .redocn.com redocn.com... URL you are allowed to connect;

If ($ invalid_referer ){

Rewrite ^/http://www.redocn.com/images/redocn.gif; // display the image you specified when someone else leeching.

# Return 403;

}
3.blog.redocn.com
[Root @ redocn vhosts] # vi blog_redocn_com.confserver

{

Listen 80;

Server_name blog.redocn.com;

Index index.html index.htm index. php;

Root/data/www/wwwroot/blog;

Error_page 404 http://bbs.redocn.com;

# Supsite rewrite

Rewrite ^ ([0-9] +)/spacelist (. *) $ index. php? $1/action_spacelist $2;

Rewrite ^ ([0-9] +)/viewspace _ (. +) $ index. php? $1/action_viewspace_itemid _ $2;

Rewrite ^ ([0-9] +)/viewbbs _ (. +) $ index. php? $1/action_viewbbs_tid _ $2;

Rewrite ^ ([0-9] +)/(. *) $ index. php? $1/$2;

Rewrite ^ ([0-9] +) $ index. php? $1;

Rewrite ^ action _ (. +) $ index. php? Action _ $1;

Rewrite ^ category _ (. +) $ index. php? Action_category_catid _ $1;

Rewrite ^ itemlist _ (. +) $ index. php? Action_itemlist_catid _ $1;

Rewrite ^ viewnews _ (. +) $ index. php? Action_viewnews_itemid _ $1;

Rewrite ^ viewthread _ (. +) $ index. php? Action_viewthread_tid _ $1;

Rewrite ^ index ([\. a-zA-Z0-9] *) $ index. php;

Rewrite ^ html/([0-9] +)/viewnews_itemid _ ([0-9] +) \. html $ index. php? Action_viewnews_itemid _ $2;

Rewrite ^/([0-9] +)/spacelist (. +) $/index. php? Uid/$1/action/spacelist/type $2;

Rewrite ^/([0-9] +)/viewspace (. +) $/index. php? Uid/$1/action/viewspace/itemid $2;

Rewrite ^/([0-9] +)/viewbbs (. +) $/index. php? Uid/$1/action/viewbbs/tid $2;

Rewrite ^/([0-9] +)/(. *) $/index. php? Uid/$1/$2;

Rewrite ^/([0-9] +) $/index. php? Uid/$1;

Rewrite ^/action (. +) $/index. php? Action $1;

Rewrite ^/category (. +) $/index. php? Action/category/catid $1;

Rewrite ^/viewnews (. +) $/index. php? Action/viewnews/itemid $1;

Rewrite ^/viewthread (. +) $/index. php? Action/viewthread/tid $1;

Rewrite ^/mygroup (. +) $/index. php? Action/mygroup/gid $1;

Location ~ . * \. Php? $

{

Include conf/enable_php5.conf;

}

}
Note: blog uses powerful Supesite as Blog site: http://www.supesite.com/
1. for more information about how to implement static Blog in Nginx, see # supesite rewrite above.

4.down.redocn.com
[Root @ redocn vhosts] # vi down_redocn_com.conflimit_zone one $ binary_remote_addr 10 m;

Server

{

Listen 80;

Server_name down.redocn.com;

Index index.html index.htm index. php;

Root/data/www/wwwroot/down;

Error_page 404/index. php;

# Redirect server error pages to the static page/50x.html

Error_page 500 502 503 x.html;

Location =/50x.html {

Root html;

}

# Zone limit

Location /{

Limit_conn one 1;

Limit_rate 20 k;

}

    

# Serve static files

Location ~ ^/(Images | javascript | js | css | Flash | media | static )/{

Root/data/www/wwwroot/down;

Expires 30d;

}

}
Note:
As the number of BT download software is growing, how can we limit the number and speed of download concurrency? Apache requires a third-party module, so nginx does not need it.
In nginx, two commands can be used: limit_zone (limit_conn) to limit the number of concurrent requests and limit_rate to limit the download speed. please refer to the above configuration instance.

5. start the nginx service/usr/local/php-fcgi/bin/spawn-fcgi-a 127.0.0.1-p 8085-C 250-u www-f/usr/local/php-fcgi/ bin/php-cgi

/Usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx. conf
You can make the above two commands into a system startup service script. There are also a lot of related scripts on the internet. This article will not post them out and provide an instance link:
Http://topfunky.net/svn/shovel/nginx/init.d/nginx

V. problem and experience summary:
1. after the Discuz Forum is installed, can I upload attachments larger than M?
Add client_max_body_size 10 m to the main configuration file. for detailed instructions, see the Wiki link provided in (6.

2. the Discuz attachment cannot be downloaded?
Recently, I encountered a strange problem that the discuz forum under nginx could not download the attachment. later I opened error_log to know that the/usr/local partition is full,
After clearing a lot of useless files, it will be normal immediately.

The above are the two minor issues I encountered during the migration and testing process. I attached a description here, but I hope that my friends who Configure nginx will not make the same mistake.
Welcome to contact NetSeek (enthusiastic linux fans ^_^ msn: cnseek@msn.com QQ: 67888954 Gtalk: cnseek@Gmail.com ).

6. Related links:
1. Discuz !. Net High Performance PHP Forum program http://www.discuz.net Supesite: http://www.supesite.com/
2. Nginx reference documents: http://wiki.codemongers.com/
3. using Nginx to achieve load balancing (a Ye eldest brother's article): http://www.imysql.com/comment/reply/210
4. linuxPk [Linux treasure house]: http://bbs.linuxpk.com
5. Red moving China Co., http://bbs.redocn.com.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.