安装nginx
1.yum install zlib zlib-devel pcre -y
2.tar zxvf pcre-8.31.tar.gz
3.cd pcre-8.31
4../configure
5.make && make install
6.useradd nginx -s /sbin/nologin -M
7.tail -1 /etc/passwd
8.tar zxf nginx-1.12.1.tar.gz
9.cd nginx-1.12.1
10.mkdir /application
11../configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module
报错:./configure: error: SSL modules require the OpenSSL library
解决:yum -y install openssl openssl-devel 再编译成功
12.make && make install
13.ln -s /application/nginx-1.12.1 /application/nginx
14./application/nginx/sbin/nginx -t 检查语法
报错少了libpcre.so.1
15.find / -name libpcre.so*
16.vim /etc/ld.so.conf
加入/usr/local/lib/
17.ldconfig
18./application/nginx/sbin/nginx -t 成功
19./application/nginx/sbin/nginx 启动
20.netstat -anlpt|grep 80
21./etc/init.d/iptables stop
22.grep html /application/nginx/conf/nginx.conf 站点
23.cd /application/nginx/html/
50x.html 错误页面优雅替代
23.cd /application/nginx/conf/
基于域名的虚拟主机
1.vim nginx.conf
user nginx nginx;
worker_processes 8; 启动进程
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root /data0/www/www;
index index.php index.html index.htm;
#access_log /app/logs/www_access.log commonlog;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
2.mkdir /data0/www/www -p
3.tree -L 1 /data0/www/
4.chown -R nginx.nginx /data0/www/
5.echo www >/data0/www/www/index.html
6./application/nginx/sbin/nginx -t 检查配置文件语法
7./application/nginx/sbin/nginx -s reload
8.lsof -i :80
9.vim /etc/hosts
192.168.1.17 www.etiantian.org 加入
简洁配置
1.cd /application/nginx/conf/
2.mkdir extra
查看nginx,extra/nginx.vhosts.conf配置文件
3. mkdir /app/log -p 加日志轮循
4.touch {www,bbs,blog}_access.log
5./application/nginx/sbin/nginx -v 查看版本
基于端口:
只需要vim nginx.vhosts.conf
server {
listen 192.168.1.17:8070;
server_name 192.168.1.17;
location / {
root /data0/www/bbs;
index index.html index.htm;
access_log /app/log/bbs_access.log commonlog;
}
}
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
报错:nginx: [emerg] bind() to 192.168.1.17:8070 failed (98: Address already in use) 1.17端口被80占用
解决:killall -9 nginx
/application/nginx/sbin/nginx
IE登录: http://192.168.1.17:8070/
cat /app/log/bbs_access.log 查看轮询日志
基于VIP:
ifconfig eth1:0 192.168.1.196 netmask 255.255.255.0 up
server {
listen 192.168.1.196:8070;
server_name 192.168.1.196;
location / {
root /data0/www/bbs;
index index.html index.htm;
access_log /app/log/bbs_access.log commonlog;
}
}
IE登录: http://192.168.1.196:8070/
Nginx多实例
1.cd /application/nginx
2.cp -a conf cmsconf
3.cd cmsconf/extra/nginx.vhosts.conf
server {
listen 8080;
server_name 192.168.1.17;
location / {
root /data0/www/blog;
index index.html index.htm;
access_log /app/log/blog_access.log commonlog;
}
}
4./application/nginx/sbin/nginx -c /application/nginx/cmsconf/nginx.conf -t
5./application/nginx/sbin/nginx -c /application/nginx/cmsconf/nginx.conf
6./application/nginx/sbin/nginx (第一个启动)
7.netstat -anlpt|grep nginx
多个nginx实例跑在普通用户下
1.useradd cms
2.tail -1 /etc/passwd
3.su - cms
4.rz fenyonghu_nginx-2.tar.gz
5.tar zxvf fenyonghu_nginx-2.tar.gz
6.tree
7.cd code/idc_product/conf/extra/
8.vim nginx_vhosts.conf
9.cd ~
10.cd bin/
11.logout
12.chmod a+w /etc/sudoers
13.vim /etc/sudoers
cms ALL=(ALL) NOPASSWD:ALL
14.chmod 400 /etc/sudoers
15.su - cms
16.cd bin/
17.sh deploy.sh
IE能登录:http://192.168.1.196:8090/
报403错误时
cd code/idc_product/conf/
vim nginx.conf
user cms cms;
nginx基本配置:模块
Nginx core modules(必须的):Main、Events
Standard HTTP modules:Core、Access、FastCGI、Gzip(压缩模块)
Log(日志模块)、proxy、Rewirte URL(重要模块)、Upstream(负载均衡模块)
cat www_access_.log日志格式解析
92.168.1.18 - - [03/Mar/2018:12:51:05 +0800] "GET / HTTP/1.1" 200 11 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" "-"
1.客户端地址 2.远程客户端用户名称 3.记录访问时间 4.HTTP协议 5.返回状态 6.发送的body字节数 7.引用文 8.客户端浏览器
cd /app/log
①awk '{print $1}' www_access_.2018-03-03.log |sort|uniq -c|sort -rn -k1 取出域名并排序
②awk '{++S[$1]} END {for(key in S) print key,S[key]}' www_access_.2018-03-03.log |sort -rn -k2
server
{
listen 80;
server_name status.etiantian.org;
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.1.17 (限制访问)
deny all;
}
}
Nginx优化
1. Expires缓存模块 见配置文件
2. Gzip压缩模块 见配置文件
3. 错误页面优雅显示 见配置文件