先安装
pcre
pcre-devel
openssl-devel
下载Nginx并解压
tar xf Nginx-1.10.1.tar.gz
cd Nginx-xxx
配置
./configure --prefix=/application/Nginx-1.10.1 --user=Nginx --group=Nginx \
--with-http_ssl_module --with-http_stub_status_module
useradd Nginx -s /bin/nologin -M && id Nginx
安装
make && make install
创建软连接(去掉版本号方便使用):
ln -s /application/Nginx-1.10.1/ /application/Nginx
启动
检查,用浏览器访问,若连接不上,检查iptable
ps -ef |grep Nginx |grep -v grep && ss -lntup |grep Nginx
curl 127.0.0.1
排错日志
/var/log/messages
/application/Nginx/logs/error.log
grep -Ev '#|^$' Nginx.conf
Nginx的参数
-t 检查配置文件语法,reload前需要先执行改命令,另外重启后需要启动检查脚本进行接口探测
-v 版本
-V 查看编译参数
-s 后面追加启动关闭信号参数,reload可以重新读取配置
配置文件配置
1、在http的标签里使用include进行分块
include extra/*.conf;
2、在其他conf文件里对虚拟主机进行配置
主配置文件
cd /application/Nginx/conf &&\
cat >Nginx.conf<<eof
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
include /application/Nginx/conf/vhosts/*.conf;
include /application/Nginx/conf/extra/*.conf;
}
eof
附属虚拟主机配置文件
mkdir -p /application/Nginx/conf/vhosts &&\
cd /application/Nginx/conf/vhosts &&\
touch www.conf &&\
touch /application/Nginx/logs/error_crit_Server1.log &&\
touch /application/Nginx/logs/access_www.log &&\
cat >www.conf<<eof
server {
listen 80;
#配置错误日志的位置和等级,可以使用默认的配置
error_log /application/Nginx/logs/error_crit_Server1.log error;
#www.bbb.com bbb.com这个是别名,利用别名可以拿来探测那个服务器访问不正常
server_name localhost www.bbb.com ;
location / {
root html/www;
index www.html index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#配置访问日志使用off可以禁用访问日志,使用的日志等级需要在主配置文件里配置好等级格式设置
access_log logs/access.log main;
}
eof
使用rewrite配置域名跳转
mkdir -p /application/Nginx/conf/vhosts &&\
cd /application/Nginx/conf/vhosts &&\
touch rewrite.conf
cat >rewrite.conf<<eof
server {
listen 80;
server_name bbb.com;
rewrite /(.*) http://www.bbb.com/\$1 permanent;
}
eof
监控主机的配置文件
mkdir -p /application/Nginx/conf/extra &&\
touch /application/Nginx/conf/extra/status.conf &&\
cat >/application/Nginx/conf/extra/status.conf<<eof
server{
listen 80;
server_name status.test.org;
location / {
stub_status on;
access_log off;
}
}
eof
将status.test.org加入hosts文件
echo "127.0.0.1 status.test.org" >>/etc/hosts
配置文件的其他参数
log_format 日志格式
access_log 可以在日志参数里加上buffer和flush提升并发性能,甚至可以通过syslog发送到其他地方(为了提高性能,可以设计成在内存里处理后只留下关键信息记录到磁盘上)
使用脚本轮巡日志,把每天的日志进行分割(重命名并清空),写入定时任务0时执行(未测试)
mkdir -p /application/Nginx/script &&\
cd /application/Nginx/script && touch test.sh &&\
cat >test.sh<<eof
#! /bin/sh
Dateformat="\$(date +%F -d -1day)"
Basedir="/application/Nginx"
Nginxlogdir="\$Basedir/logs"
Logname="access_www.log"
[ -d \$Nginxlogdir ] && cd \$Nginxlogdir ||exit 1
[ -f \$Logname ] || exit 2
cd \$Nginxlogdir
/bin/cp \$Nginxlogdir/\$Logname \$Nginxlogdir/\${Dateformat}_\${Logname}
>\$Nginxlogdir/\$Logname
eof