2014-04-01 09:32:01
来 源
itjs.cn
Nginx
本文介绍CentOS系统安装配置Nginx服务器以及简单优化,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
1、环境准备

Centos 5.4

Nginx 1.2.1 : wget http://www.nginx.org/download/nginx-1.2.1.tar.gz

2: 安装步骤

#安装gcc编译器及相关工具 yum -y install gcc gcc-c++ autoconf automake make

#安装相关依赖的模块 yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

# cd /usr/local/src

# wget http://www.nginx.org/download/nginx-1.2.1.tar.gz

# tar -zxvf nginx-1.2.1.tar.gz            // 解压包

#./configure --prefix=/usr/local/nginx         //通过编译源码的方式进行安装

#make

#make install

配置文件:(配置和注释收集与网络)

#/usr/local/nginx/conf/nginx.config 

-----------------------------------------nginx.config----------------------------------------------------------------

#运行用户

#user  nobody;

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

#全局错误日志及PID文件

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

#工作模式及连接数上限

events {

use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

worker_connections  1024;#单个后台worker process进程的最大并发链接数

# multi_accept on;

}

http {

#设定mime类型,类型由mime.type文件定义

include       /etc/nginx/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  /var/log/nginx/access.log main

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,

#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

sendfile        on;

#tcp_nopush     on;

#连接超时时间

#keepalive_timeout  0;

keepalive_timeout  65;

tcp_nodelay        on;

#开启gzip压缩

gzip  on;

gzip_disable "MSIE [1-6].(?!.*SV1)";

}

server {

#侦听80端口

listen       80;

#定义使用www.xx.com访问

server_name  www.xx.com;

#设定本虚拟主机的访问日志

access_log  logs/www.xx.com.access.log  main;

#默认请求

location / {

root   /usr/local/nginx/html;      #定义服务器的默认网站根目录位置

index index.php index.html index.htm;   #定义首页索引文件的名称

}

# 定义错误提示页面

error_page   500 502 503 504 /50x.html;

location = /50x.html {

root   /usr/local/nginx/html;

}

------------------------------------------------------------------------------------------------------------------------------------

3、如何启动Nginx

#启动nginx

#./usr/local/nginx/sbin/nginx -t

显示以下信息为正确的

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动成功后,查看nginx进程信息: # ps -ef | grep nginx ,看是否存在nginx的进程来确认是否成功启动。 #重启动nginx

#./usr/local/nginx/sbin/nginx -s reload

测试http://www.xx.com/

同时记得检查centos防火墙设置,是否开启了相应端口,可使用setup命令来设置防火墙、dns、网络等信息。

如果默认的配置文件未做任何改动,使用浏览器直接访问nginx server,会出现提示:Welcome to Nginx

4、优化

vim /etc/sysctl.conf

在最后添加(待详细研究)

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog = 32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.ip_local_port_range = 1024  65535

保存退出后执行

sysctl -p

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