-
安裝基礎包
yum -y install gcc gcc-c++ lrzsz wget vim
-
安裝完成, 查看gcc版本
gcc -v
輸出:gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
-
需要安裝 spenssl
yum install -y openssl openssl-devel
如果沒有可能會報如下錯誤:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
-
獲取Nginx
wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
-
解壓 & 進入目錄
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
-
使用默認位置
./configure
-
編譯安裝
make
make install
-
找到Nginx的目錄
whereis nginx
-
啟動Nginx
/usr/local/nginx/sbin/nginx
Nginx 操作指令
nginx -c /etc/nginx/nginx.conf (啟動Nginx)
nginx -s stop (快速停止)
nginx -s quit (完整有序的停止)
nginx -s reload (重新加載)
nginx -s reopen (重新打開日志文件)
nginx -s quit
kill -s QUIT 1628
ps -ax|grep nginx (獲取所有正在運行的Nginx進程列表)
nginx -t (驗證配置是否有語法錯誤)
nginx -v (查看Nginx的簡介版本號)
nginx -V (查看Nginx的詳細版本號)
/usr/local/nginx/sbin/nginx -t (檢查配置文件是否正確)
/usr/local/nginx/sbin/nginx -s reload (刷新)
獲取所有正在運行的Nginx進程列表
ps -ax|grep nginx
配置 Nginx 開機啟動
修改權限:chmod 755 /etc/rc.d/rc.local
編輯:vim /etc/rc.d/rc.local
添加 /usr/local/nginx/sbin/nginx
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
Nginx配置文件的結構及說明:
# 運行用戶
user root;
# 工作進程的數量 通常設置和CPU數量相等 auto:自動調整
worker_processes auto;
# 全局的錯誤日志
error_log /var/log/nginx/error.log;
# 日志級別, 從左到右(由低到高): debug做詳細, crit最少, 默認是: crit
# debug|info|notice|warn|error|crit|alert|emerg:
# 生產場景一般用: warn|error|crit; 注意: 不要配置info級別較低的等級, 會帶來大量的磁盤I/O消耗
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 日志沒有配置 默認
#error_log logs/error.log error;
# 不記錄日志可以這么寫
#error_log /dev/null
# PID文件 記錄Nginx主進程的ID號
pid /run/nginx.pid;
# 指定工作模式及連接數上限
events {
# use 用來指定Nginx的工作模式
# Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll
# epoll是多路復用IO(I/O Multiplexing)中的一種方式
# 僅用於linux2.6以上內核,可以大大提高nginx的性能
use epoll;
# 單個工作進程進程可以允許同時建立外部連接的數量
# 進程的最大並發連接數: 並發總數 = worker_processes * worker_connections;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 設定日志格式
# 格式變量解釋:
# $remote_addr: 記錄訪問網站的客戶端地址
# $remote_user: 遠程客戶端用戶名
# $time_local: 記錄訪問時間和時區
# $request: 用戶的Http請求起始行信息
# $status: http狀態碼, 記錄請求返回的狀態碼
# $body_bytes_sent: 服務器發送給客戶端的相應body字節數
# $http_referer: 記錄此次請求是從那個連接訪問過來的, 可以根據此參數進行防盜鏈設置
# $http_user_agent: 記錄客戶端訪問信息, 例如:瀏覽器、手機客戶端等
# $http_x_forwarded_for: 當前端有代理服務時, 設置Web節點記錄客戶端地址的配置, 此參數生效的前提是代理服務器也要進行相關的x_forwarded_for設置
#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 logs/access.log main
# sendfile 是Linux 2.0+ 以后推出的一個系統調用
# sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來傳輸文件
# 對於普通應用, 必須設為 on
# 如果用來進行下載等應用磁盤IO重負載應用, 可設置為 off
# 對比一般的數據網絡傳輸sendfile會有更少的切換和更少的數據拷貝
# 在nginx配置文件里面, 打開sendfile on選項能夠提高 web server 性能
sendfile on;
# 在nginx中, tcp_nopush必須和sendfilep搭配使用, 它可以配置一次發送數據包的大小, 它不是按時間累計0.2秒后發送包, 而是當包累計到一定大小后就發送
# 在nginx中, tcp_nopush和tcp_nodelay是互斥的
#tcp_nopush on;
#tcp_nodelay on;
# 連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
# 開啟 gzip 壓縮 (on | off) 默認: off
gzip on;
# 設置用於處理請求壓縮的緩沖區數量和大小
gzip_buffer 32 4k | 16 8k
# 壓縮級別(1-9), 越大的壓縮率越高, 同時消耗CPU資源也越多
gzip_comp_level 3;
# 壓縮協議版本, 默認是1.1 (1.0 | 1.1)
gzip_http_version 1.1;
# 小於1k的資源不壓縮
gzip_min_length 1k;
# 當nginx處於反向代理的時候啟用(off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...)
# off: 禁止所有代理請求的壓縮
# expired: 在Header中包含'Expires'頭信息, 啟用壓縮
# no-cache: 在Header中包含'Cache-Control:no-cache'頭信息, 啟用壓縮
# no-store: 在Header中包含'Cache-Control:no-store'頭信息, 啟用壓縮
# private: 在Header中包含'Cache-Control:private'頭信息, 啟用壓縮
# no_last_modified: 在Header中包含'Last-Modified'頭信息, 啟用壓縮
# no_etag: 在Header中包含'ETag'頭信息, 啟用壓縮
# auth: 在Header中包含'Authorization'頭信息, 啟用壓縮
# any: 無條件壓縮所有結果數據
# 默認: off
gzip_proxied off;
# 需要壓縮哪些響應類型的資源, 多個空格隔開, 不建議壓縮圖片(mime-type ...)
# 默認: text/html
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
# 配置禁用gzip條件, 支持正則, 此處表示IE6以下不開啟gzip, 因為低版本的IE不支持
gzip_disable "MSIE [1-6].";
# 是否添加"Vary:Accept-Encoding"響應頭(on | off)
# 默認: off
gzip_vary on;
# 設置客戶端請求頭的緩沖區大小
client_header_buffer_size 1k;
# 讀取客戶端讀取請求頭超時時間
client_header_timeout 60s;
# 設置用於讀取大型客戶端請求頭的緩沖區的最大數 和 大小
large_client_header_buffers 4 8k;
# 設定虛擬主機配置
server {
# 偵聽端口
listen 80 default_server;
# 服務的名稱: 使用example.net www.example.net訪問
server_name example.net www.example.net;
# 設置網站在服務器上的根目錄
root /data/www
# 設置緩沖日志寫入的路徑 格式 和 配置
# 默認: access_log logs/access.log combined;
access_log
# 默認請求
location = / {
# 定義首頁索引文件的名稱
# 默認: index index.html;
index index.html;
}
# 定義錯誤頁面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}