Web服务器nginx虚拟主机与反向代理 web 服务器 - nginx
web服务器简介:
apache,web服务器:访问网页,查找、浏览信息。 蜘蛛程序-网络爬虫
常见的web服务器: apache lighttpd nginx tomcat IIS
介绍nginx:
高性能的http服务器和反向代理服务器(web加速),运行在类unix和windows上
为什么选择nginx?
处理速度快,占用的资源少
apache里的模块是动、静结合;在nginx里面,都是静态的。 支持热部署
可以 7x24 不间断运行
书写的代码质量很高,也很规范
问题:俄国人写的,一些官方资料,文档比较少。
============================================= 安装之前: 1、准备工作:
1)apache是停止的,释放80端口
2)添加一个普通用户,出于安全的目的使用这个普通用户去运行nginx # useradd -M -s /sbin/nologin www
默认使用的是 nobody 这个用户。
2、开始安装
[root@localhost lnmp]# ls nginx--x.tar.gz nginx--x.tar.gz
[root@localhost lnmp]# tar zxvf nginx--x.tar.gz -C /usr/local/src/
安装nginx所需的软件包: pcre-8.10.tar.gz nginx-0.8.46.tar.gz
(1)PCRE:Perl库, Perl Compatible Regular Expressions 支持正则表达式
[root@localhost nginx]# tar zxvf pcre-8.10.tar.gz
[root@localhost nginx]# cd pcre-8.10 [root@localhost pcre-8.10]# ls
[root@localhost pcre-8.10]# ./configure [root@localhost pcre-8.10]# make
[root@localhost pcre-8.10]# make install
(2)安装 nginx
[root@localhost nginx]# ls nginx-0.8.46.tar.gz nginx-0.8.46.tar.gz
[root@localhost nginx]# tar zxvf nginx-0.8.46.tar.gz
[root@localhost nginx-0.8.46]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --user --group --prefix --with-http_stub_status_module 状态模块 --with-http_ssl_module 支持 https yum install -y openssl-devel
[root@localhost nginx-0.8.46]# make
[root@localhost nginx-0.8.46]# make install
[root@localhost nginx-0.8.46]# cd /usr/local/nginx/ [root@localhost nginx]# ls conf html logs sbin
启动服务:
[root@localhost nginx]# ./sbin/nginx -c conf/nginx.conf
[root@localhost nginx]# netstat -antp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19449/nginx.conf
访问:http://192.168.7.253/ Welcome to nginx! OK
解读配置文件:
[root@localhost conf]# pwd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf 1
2 user www;
3 worker_processes 1; #开启的进程数,与 CPU的核数一致,
查看CPU的信息:
[root@localhost html]# cat /proc/cpuinfo
5 #error_log logs/error.log; 错误日志 6 #error_log logs/error.log notice; 7 #error_log logs/error.log info;
redhat 5 man 5 syslog.conf redhat 6 man 5 rsyslog.conf ?
9 #pid logs/nginx.pid;**
12 events {
13 use epoll; #指定nginx使用高效的工作模式
14 worker_connections 1024; # 每个进程能够处理的最大连接数 15 } 能够处理的最大连接数=worker_processes x worker_connections use epoll; 指定nginx的工作模式 默认使用的是 select 和 poll epoll 2.6内核之后,某些发行版,比如 SUSE、redhat 支持 epoll模型
# uname -r 2.6.18-194.el5
linux 和 redhat 和 windows 有没有可比性? 其他的发行版,要想使用高效模式:kqueue BSD系列MacOS 等
# cat /etc/issue
Red Hat Enterprise Linux Server release 5.5 (Tikanga)
18 http {
19 include mime.types; #文件的扩展名和文件类型
20 default_type application/octet-stream; #文件的类型默认是二进制
22-26 访问日志
28 sendfile on; #开启高效的文件传输模式 29 #tcp_nopush on; #防止网络阻塞
31 #keepalive_timeout 0; #长连接的超时时间,秒 32 keepalive_timeout 65;
34 #gzip on; #开启gzip压缩传输数据
36 server {
37 listen 80;
38 server_name localhost; 39
40 #charset koi8-r; #字符集 41
42 #access_log logs/host.access.log main; 43
44 location / {
45 root html; == documentroot 46 index index.html index.htm; 47 } } }
页面文件的名字
虚拟主机: 基于域名的虚拟主机。 FQDN Documentroot ww1.nginx.com /htdocs/ww1 ww2.nginx.com /htdocs/ww2 default /htdocs/default
日志 迁移: 从 /usr/local/nginx/logs --> /logs(raid0)
# mkdir -p /htdocs/{ww1,ww2,default} -v 注意权限: www 进去 x
[root@localhost conf]# cd /htdocs/ww1
[root@localhost ww1]# echo \
[root@localhost ww1]# cd ../ww2
[root@localhost ww2]# echo \[root@localhost ww2]# cd ../default/
[root@localhost default]# echo \
测试配置文件是否正确:
[root@localhost 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
重启: 用 kill 命令 给 nginx 的pid 传递一个信号,让它重新读取配置文件。
[root@localhost nginx]# cat logs/nginx.pid 19449
nginx的平滑重启:
[root@localhost nginx]# kill -HUP 19449
客户端测试: vim /etc/hosts 添加
192.168.7.253 ww1.nginx.com 192.168.7.253 ww2.nginx.com
打开浏览器: http://ww1.nginx.com/ ww1.nginx.com http://ww2.nginx.com/ ww2.nginx.com http://192.168.7.253/ default.nginx.com
----------------
nginx的日志管理 ----------------
错误日志:nginx出问题,查看错误日志,排错 访问日志:查看有哪些客户端来访问自己
log_format ww1 '$remote_addr - $remote_user [$time_local] \ '$status $body_bytes_sent \ '\
access_log logs/ww1.log ww1;