先安裝
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
創建nginx用戶
useradd nginx -s /bin/nologin -M && id nginx
安裝
make && make install
創建軟連接(去掉版本號方便使用):
ln -s /application/nginx-1.10.1/ /application/nginx
啓動
/application/nginx/sbin/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