Fork me on GitHub
Suzf  Blog

Tag Nginx

Nginx之基本配置

Nginx

1. 高性能的HTTP Server,支持高达20000并发访问
2. 反向代理服务器,给网站加速
3. 做为前端一个负载均衡器
========================================================

一、准备工作
# service httpd stop
# chkconfig httpd off
所需的软件: 开发库,openssl-devel

二、部署Nginx
1. pcre: 支持正则表达式,地址重写rewrite
# tar zxf pcre-8.31.tar.gz
# cd pcre-8.31
# ./configure && make && make install
# ldconfig

2. nginx
# useradd www
# tar xvf nginx-1.2.2.tar.gz
# cd nginx-1.2.2
[[email protected] nginx-1.2.2]# ./configure \
> --user=www \
> --group=www \
> --prefix=/usr/local/nginx-1.2.2 \
> --with-http_stub_status_module \
> --with-http_sub_module \
> --with-http_ssl_module \
> --with-pcre=/usr/src/pcre-8.31 #pcre源程序目录

#--with-pcre=/usr/src/pcre-8.31 指的是pcre-8.31 的源码路径。
#--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

# make && make install
# ln -s /usr/local/nginx-1.2.2/ /usr/local/nginx

# tree /usr/local/nginx/
/usr/local/nginx/
|-- conf
| |-- fastcgi.conf
| |-- fastcgi.conf.default
| |-- fastcgi_params
| |-- fastcgi_params.default
| |-- koi-utf
| |-- koi-win
| |-- mime.types
| |-- mime.types.default
| |-- nginx.conf //主配置文件
| |-- nginx.conf.default
| |-- scgi_params
| |-- scgi_params.default
| |-- uwsgi_params
| |-- uwsgi_params.default
| `-- win-utf
|-- html
| |-- 50x.html
| `-- index.html
|-- logs
`-- sbin
`-- nginx

3. 启动
[[email protected] ~]# /usr/local/nginx/sbin/nginx
[[email protected] ~]# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10627/nginx

4. 测试
# links -dump 192.168.9.110
Welcome to nginx!
# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local #开机运行

二、Nginx基本配置
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
CoreModule Nginx内核模块
EventsModule 事件驱动模块
HttpCoreModule http内核模块

# sed -i '/^[ \t]*#/d;/^[ \t]*$/d' /usr/local/nginx/conf/nginx.conf

# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; #初始启动的进程数(建议CPU的core数)
worker_connections 15000; #最大连接数
server {
listen 80; #监听的端口
server_name localhost; #站点名称
location / {
root html; #root指令指定网站主目录(相对于nginx的安装目录)
index index.html index.htm; #默认主页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

语法检查:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载nginx:
[[email protected] ~]# pgrep nginx
5693
5757

[[email protected] ~]# kill -HUP 5693
[[email protected] ~]# pgrep nginx
5693
5787

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

# /usr/local/nginx/html/ #站点默认主目录
# echo "Welcome to Jeffery Nginx" > /usr/local/nginx/html/index.html
[[email protected] ~]# elinks -dump 192.168.9.110
Welcome to Jeffery Nginx

========================================================

排错:
[[email protected] ~]# /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot
open shared object file: No such file or directory

编译nginx指定 --with-pcre=/usr/src/pcre-8.31 指定pcre-8.31 的源码路径。

========================================================