1、查看系統版本/安裝常用軟件(系統最小化安裝)
[root@qfedu.com ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
1、安裝編譯環境
[[email protected] ~]# yum -y install gcc gcc-c++
2、安裝pcre軟件包(使nginx支持http rewrite模塊)
[[email protected] ~]# yum install -y pcre pcre-devel
3、安裝 openssl-devel(使 nginx 支持 ssl)
[[email protected] ~]# yum install -y openssl openssl-devel
4、安裝zlib
[[email protected] ~]# yum install -y zlib zlib-devel gd gd-devel
5、創建用戶 nginx
[[email protected] ~]# useradd -s /sbin/nologin nginx
6、編譯安裝 nginx
1、下載安裝包
[root@qfedu.com ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@qfedu.com ~]# tar -zxvf nginx-1.16.0.tar.gz
[root@qfedu.com ~]# cd nginx-1.16.0
2、編譯配置
[root@qfedu.com nginx-1.16.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module
[root@qfedu.com nginx-1.16.0]# make && make install
[root@qfedu.com nginx-1.16.0]# cd /usr/local/nginx/sbin
[root@qfedu.com sbin]# ./nginx # 啓動Nginx
[root@qfedu.com sbin]# ./nginx -t # 驗證配置文件是正確
[root@qfedu.com sbin]# ./nginx -s reload # 重啓Nginx
[root@qfedu.com sbin]# ./nginx -s stop # 停止Nginx
[root@qfedu.com sbin]# ./nginx -v # 查看是否安裝成功
nginx version: nginx/1.16.0
[root@qfedu.com sbin]# netstat -ntlp | grep nginx # 查看是否啓動
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20949/nginx: master
3、配置 Nginx 命令和服務並開機啓動
1、創建服務配置文件
[root@qfedu.com sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit] #對服務的說明
Description=nginx - high performance web server #描述服務
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target #描述服務類別
[Service] #服務的一些具體運行參數的設置
Type=forking #後臺運行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid #PID文件的路徑
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf #啓動準備
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #啓動命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload #重啓命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit #快速停止
PrivateTmp=true #給服務分配臨時空間
[Install]
WantedBy=multi-user.target #服務用戶的模式
2、添加執行權限
[root@qfedu.com sbin]# chmod +x /usr/lib/systemd/system/nginx.service
3、啓動服務(先停止nginx服務)
[root@qfedu.com sbin]# systemctl daemon-reload
[root@qfedu.com sbin]# systemctl start nginx.service # 啓動
[root@qfedu.com sbin]# systemctl stop nginx.service # 停止
[root@qfedu.com sbin]# systemctl reload nginx.service # 修改配置後重新加載生效
[root@qfedu.com sbin]# systemctl restart nginx.service # 重啓
[root@qfedu.com sbin]# systemctl status nginx # 查看服務是否啓動
4、添加開機啓動
[[email protected] sbin]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
4、防火牆默認開啓,加入放行端口80
[root@qfedu.com sbin]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@qfedu.com sbin]# firewall-cmd --reload
success
7、Nginx 編譯參數
# 查看 nginx 安裝的模塊
[[email protected] ~]# nginx -V
# 模塊參數具體功能
--with-cc-opt='-g -O2 -fPIE -fstack-protector' # 設置額外的參數將被添加到CFLAGS變量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/share/nginx # 指向安裝目錄
--conf-path=/etc/nginx/nginx.conf # 指定配置文件
--http-log-path=/var/log/nginx/access.log # 指定訪問日誌
--error-log-path=/var/log/nginx/error.log # 指定錯誤日誌
--lock-path=/var/lock/nginx.lock # 指定lock文件
--pid-path=/run/nginx.pid # 指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body # 設定http客戶端請求臨時文件路徑
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi # 設定http fastcgi臨時文件路徑
--http-proxy-temp-path=/var/lib/nginx/proxy # 設定http代理臨時文件路徑
--http-scgi-temp-path=/var/lib/nginx/scgi # 設定http scgi臨時文件路徑
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi # 設定http uwsgi臨時文件路徑
--with-debug # 啓用debug日誌
--with-pcre-jit # 編譯PCRE包含“just-in-time compilation”
--with-ipv6 # 啓用ipv6支持
--with-http_ssl_module # 啓用ssl支持
--with-http_stub_status_module # 獲取nginx自上次啓動以來的狀態
--with-http_realip_module # 允許從請求標頭更改客戶端的IP地址值,默認爲關
--with-http_auth_request_module # 實現基於一個子請求的結果的客戶端授權。如果該子請求返回的2xx響應代碼,所述接入是允許的。如果它返回401或403中,訪問被拒絕與相應的錯誤代碼。由子請求返回的任何其他響應代碼被認爲是一個錯誤。
--with-http_addition_module # 作爲一個輸出過濾器,支持不完全緩衝,分部分響應請求
--with-http_dav_module # 增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法 默認關閉,需編譯開啓
--with-http_geoip_module # 使用預編譯的MaxMind數據庫解析客戶端IP地址,得到變量值
--with-http_gunzip_module # 它爲不支持“gzip”編碼方法的客戶端解壓具有“Content-Encoding: gzip”頭的響應。
--with-http_gzip_static_module # 在線實時壓縮輸出數據流
--with-http_image_filter_module # 傳輸JPEG/GIF/PNG 圖片的一個過濾器)(默認爲不啓用。gd庫要用到)
--with-http_spdy_module # SPDY可以縮短網頁的加載時間
--with-http_sub_module # 允許用一些其他文本替換nginx響應中的一些文本
--with-http_xslt_module # 過濾轉換XML請求
--with-mail # 啓用POP3/IMAP4/SMTP代理模塊支持
--with-mail_ssl_module # 啓用ngx_mail_ssl_module支持啓用外部模塊支持
注:此時可能會出現這樣一個問題
-bash: nginx: command not found
這是由於未指定環境變量
[root@localhost sbin]# vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost sbin]# source /etc/profile #刷新
8、Nginx 配置文件
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
# 全局參數設置
user nginx/www; # 運行進程的用戶
worker_processes 1; # 設置nginx啓動進程的數量,一般設置成與邏輯cpu數量相同
error_log /var/log/nginx/error.log warn; # 指定錯誤日誌(建議絕對路勁)
worker_rlimit_nofile 102400; # 設置一個nginx進程能打開的最大文件數
pid /var/run/nginx.pid;
events { # 事件配置
worker_connections 10240; # 設置一個進程的最大併發連接數
use epoll; # 事件驅動類型
} usr
# http 服務相關設置
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #設置訪問日誌的位置和格式
sendfile on; # 用於開啓文件高效傳輸模式,一般設置爲on,若nginx是用來進行磁盤IO負載應用時,可以設置爲off,降低系統負載
tcp_nopush on; # 減少網絡報文段數量,當有數據時,先彆着急發送, 確保數據包已經裝滿數據, 避免了網絡擁塞
tcp_nodelay on; # 提高I/O性能,確保數據儘快發送, 提高可數據傳輸效率
gzip on; # 是否開啓 gzip 壓縮
keepalive_timeout 65; # 設置長連接的超時時間,請求完成之後還要保持連接多久,不是請求時間多久,目的是保持長連接,減少創建連接過程給系統帶來的性能損耗,類似於線程池,數據庫連接池
types_hash_max_size 2048; # 影響散列表的衝突率。越大,就會消耗更多的內存,但散列key的衝突率會降低,檢索速度就更快。越小,消耗的內存就越小,但散列key的衝突率可能上升
include /etc/nginx/mime.types; # 關聯mime類型,關聯資源的媒體類型(不同的媒體類型的打開方式)
default_type application/octet-stream; # 根據文件的後綴來匹配相應的MIME類型,並寫入Response header,導致瀏覽器播放文件而不是下載
# 虛擬服務器的相關設置
server {
listen 80; # 設置監聽的端口
server_name localhost; # 設置綁定的主機名、域名或ip地址
charset utf8; # 設置編碼字符
location / {
root /var/www/nginx; # 設置服務器默認網站的根目錄位置
index index.html index.htm; # 設置默認打開的文檔
}
error_page 500 502 503 504 /50x.html; # 設置錯誤信息返回頁面
location = /50x.html {
root html; # 這裏的絕對位置是/var/www/nginx/html
}
}
}
9、檢測 nginx 配置文件是否正確
[[email protected] ~]# usr/local/nginx/sbin/nginx -t
10、啓動 nginx 服務
[[email protected] ~]# /usr/local/nginx/sbin/nginx
11、Nginx 命令控制
nginx -c /usr/local/nginx/conf/nginx.conf # 以特定目錄下的配置文件啓動nginx
nginx -s reload # 修改配置後重新加載生效
nginx -s reopen # 重新打開日誌文件
nginx -s stop # 快速停止nginx
nginx -s quit # 完整有序的停止nginx
nginx -t # 測試當前配置文件是否正確
nginx -t -c /usr/local/nginx/conf/nginx.conf # 測試特定的nginx配置文件是否正確
12、腳本實現 nginx 開機自啓
1、添加啓動腳本
[[email protected] ~]# vim /etc/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
2、添加權限
[[email protected] ~]# chmod +x /etc/init.d/nginx
3、重載系統啓動文件
[[email protected] ~]# systemctl daemon-reload
4、設置開機自啓
[[email protected] ~]# systemct start nginx
13、nginx 日誌文件詳解
-
nginx 日誌文件分爲 log_format 和 access_log 兩部分
-
log_format 定義記錄的格式,其語法格式爲
-
log_format 樣式名稱 樣式詳情
-
配置文件中默認有
log_format main 'remote_addr - remote_user [time_local] "request" '
'status body_bytes_sent "$http_referer" '
'"http_user_agent" "http_x_forwarded_for"';
點擊這裏 | 點擊這裏 |
---|---|
變量 | 說明 |
http_x_forwarded_for | 客戶端的ip |
$remote_user | 客戶端的名稱 |
$time_local | 訪問時的本地時間 |
$request | 請求的URL和http協議 |
$status | 訪問的狀態碼 |
$body_bytes_sent | 發送給客戶端的主體內容大小 |
$http_referer | 記錄客戶端是從哪個頁面鏈接訪問過來的,若沒有鏈接,則訪問‘-’ |
$http_user_agent | 記錄客戶端使用的瀏覽器的相關信息 |