2014-04-11 10:57:01
来 源
itjs.cn
Nginx
本篇分享了Nginx服务器配置优化方法,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
今天别人说网站进行了一点优化

user  www www; #Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。

worker_processes 1;

error_log  /data1/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

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

worker_rlimit_nofile 51200;

events

{

use epoll;

worker_connections 51200;

}

http

{

incl?       mime.types;

default_type  application/octet-stream;

#charset  gb2312;

server_names_hash_b ket_size 128;

client_header_b?r_size 32k;

large_client_header_b?rs 4 32k;

sendfile on;

tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_b?r_size 64k;

fastcgi_b?rs 4 64k;

fastcgi_busy_b?rs_size 128k;

fastcgi_temp_file_write_size 128k;

#对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。

gzip on;

gzip_min_length  1k;

gzip_b?rs     4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_types       text/plain application/x-javascript text/css application/xml;

gzip_vary on;

#limit_zone  crawler  $binary_remote_addr  10m;

server

{

   listen       80;

   server_name  www.server110.com server110.com *.server110.com;

   index index.html index.htm index.php;

   root  /data0/htdocs/server110;

   #limit_conn   crawler  20;    

   #针对Bo-Blog系统的Rewrite静态化

   rewrite ^/post/([0-9]+).htm$ /read.php?$1 last;

   rewrite ^/post/([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2 last;

   rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2?=$3 last;

   rewrite ^/index_([0-9]+)_([0-9]+).htm$ /index.php?mode=$1&page=$2 last;

   rewrite ^/star_([0-9]+)_([0-9]+).htm$ /star.php?mode=$1&page=$2 last;

   rewrite ^/category_([0-9]+).htm$ /index.php?go=category_$1 last;

   rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=category_$1&mode=$2&page=$3 last;

   rewrite ^/archive_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2 last;

   rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;

   rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3 last;

   rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;

   location ~ .*.(php|php5)?$

   {

#将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。

fastcgi_pass  unix:/tmp/php-cgi.sock;

#fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php;

incl? fcgi.conf;

   }

   location ~ /read.php

   {

#将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。

fastcgi_pass  unix:/tmp/php-cgi.sock;

#fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php;

incl? fcgi.conf;

   }

   #博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。

   location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

   {

expires      15d;

   }

   #博客会加载很多JavaScript、CSS,将它们在浏览器本地缓存1天,访问者在看完一篇文章或一页后,再看另一篇文件或另一页的内容,无需从服务器再次下载相同的JavaScript、CSS,提高了页面显示速度。

   location ~ .*.(js|css)?$

   {

expires      1d;

   }  

   log_format  access  '$remote_addr - $remote_user [$time_local] "$reqst" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

access_log  /data1/logs/access.log  access;

   }

}


  三、PHP 5.2.6(FastCGI)的配置优化

  1、php.ini 配置文件中关于eAcelerator的优化。只使用1M共享内存,删除所有在最后3600秒内无法存取的脚本缓存,用磁盘辅助进行缓存。

[eaccelerator]

zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"

eaccelerator.shm_size="1"

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="3600"

eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

eaccelerator.keys = "disk_only"

eaccelerator.sessions = "disk_only"

eaccelerator.content = "disk_only"

  2、php-fpm.conf 的配置优化

  修改两项,一是修改以下一行,将启动的php-cgi进程数由原来的128个改为5个:

<val name="max_children">5</val>

  二是修改以下一行,将TCP模式改为Unix Socket模式:

<val name="listen_address">/tmp/php-cgi.sock</val>


  四、MySQL 5.1.26 配置优化

  1、使用以下参数编译安装的 MySQL 5.1 默认支持4种存储引擎:CSV、MRG_MYISAM、MEMORY、MyISAM,不支持InnoDB存储引擎。由于内存有限,而InnoDB耗费的内存较大,这里推荐使用MyISAM存储引擎。 ./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile

make && make install

  2、MySQL 5.1 配置文件(my.cnf)优化

[client]

port    = 3306

socket  = /tmp/mysql.sock [mysql]

prompt="(u:s135:)[d]> "

no-auto-rehash

[mysqld]

user    = mysql

port    = 3306

socket  = /tmp/mysql.sock

basedir = /usr/local/webserver/mysql

datadir = /usr/local/webserver/mysql/data

open_files_limit    = 600

back_log = 20

max_connections = 100

max_connect_errors = 200

table_cache = 60

external-locking = FALSE

max_allowed_packet = 16M

sort_b?r_size = 128K

join_b?r_size = 128K

thread_cache_size = 10

thread_concurrency = 8

qry_cache_size = 0M

qry_cache_limit = 2M

qry_cache_min_res_unit = 2k

default_table_type = MyISAM

thread_stack = 192K

transaction_isolation = READ-UNCOMMITTED

tmp_table_size = 512K

max_heap_table_size = 32M

/usr/local/webserver/mysql/data/slow.log

/usr/local/webserver/mysql/data/error.log

long_qry_time = 1

log_long_format

server-id = 1

#log-bin = /usr/local/mysql/data/binlog

binlog_cache_size = 2M

max_binlog_cache_size = 4M

max_binlog_size = 512M

expire_logs_days = 7

key_b?r_size = 4M

read_b?r_size = 1M

read_rnd_b?r_size = 2M

bulk_insert_b?r_size = 2M

myisam_sort_b?r_size = 4M

myisam_max_sort_file_size = 10G

myisam_max_extra_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

[mysqldump]

quick

max_allowed_packet = 16M

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。