1、準(zhǔn)備安裝環(huán)境
rpm -qa|grep gcc
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2料仗、下載編譯包
https://nginx.org/en/download.html
我用的是nginx-1.14.2.tar.gz
# 解壓
tar -xvf nginx-1.14.2.tar.gz
# 進(jìn)入目錄
cd nginx-1.14.2
交叉編譯
./configure --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
make
make && make install
make 完成后默認(rèn)會(huì)在/usr/local下生成一個(gè)nginx目錄
3、配置nginx.conf
Nginx 編譯參數(shù)
-
--prefix=path
—— 定義Nguni 服務(wù)所在的文件夾诀浪,服務(wù)根路徑筑辨,用于設(shè)置其他由configure設(shè)置的相對(duì)路徑(除了用到的源庫(kù)路徑)纯蛾。默認(rèn)是/usr/local/nginx
。 -
--sbin-path=path
—— 設(shè)置nginx可執(zhí)行文件的名字却嗡,默認(rèn)是prefix/sbin/nginx
舶沛。 -
--conf-path=path
—— 設(shè)置nginx配置文件的名字。nginx可以通過(guò)指定使用不同的配置文件來(lái)啟動(dòng)窗价,加命令參數(shù)為-c file如庭。默認(rèn)nginx配置文件名為prefix/conf/nginx.conf。 -
--pid-path=path
—— 設(shè)置保存nginx主進(jìn)程process ID的文件名撼港。安裝完畢后也可以在nginx.conf中修改,使用pid選項(xiàng)坪它。該文件默認(rèn)命名為prefix/logs/nginx.pid。 -
--error-log-path=path
—— 設(shè)置關(guān)鍵錯(cuò)誤帝牡、警告和診斷的日志文件名往毡。安裝完畢后也可以在nginx.conf中修改,使用error_log選項(xiàng)靶溜。該文件默認(rèn)命名為prefix/logs/error.log开瞭。 -
--http-log-path=path
—— 設(shè)置http服務(wù)請(qǐng)求的日志文件名。安裝完畢后罩息,可在nginx.conf中修改嗤详,使用access_log選項(xiàng)。默認(rèn)文件名prefix/logs/access.log瓷炮。 -
--build=name
—— 設(shè)置一個(gè)可選的nginx別名 -
--user=name
—— 設(shè)置工作進(jìn)程的用戶(hù)名(一般是非特權(quán)用戶(hù))葱色,安裝完畢后,可在nginx.conf中通過(guò)選項(xiàng)user修改娘香。默認(rèn)為nobody苍狰。 -
--group=name
—— 設(shè)置工作進(jìn)程的用戶(hù)組办龄,安裝完畢后,可在nginx.conf中通過(guò)選項(xiàng)user修改舞痰。默認(rèn)情況下土榴,用戶(hù)組名稱(chēng)是非特權(quán)用戶(hù)組名稱(chēng)。 --with-select_module
-
--without-select_module
—— 啟用或禁用一個(gè)允許服務(wù)使用select()方法的模塊响牛。如果平臺(tái)沒(méi)有更適合的方法玷禽,例如kqueue, epoll, or /dev/poll,這個(gè)模塊會(huì)自動(dòng)編譯呀打。
我的理解是:--with-select_module表示將編譯select_module模塊 -
--without-select_module
表示不編譯select_module模塊 --with-poll_module
-
--without-poll_module
—— 啟用或禁用一個(gè)允許服務(wù)使用poll()方法的模塊矢赁。如果平臺(tái)沒(méi)有更適合的方法,例如kqueue, epoll, or /dev/poll贬丛,這個(gè)模塊會(huì)自動(dòng)編譯撩银。 -
--without-http_gzip_module
—— 禁止編譯壓縮http服務(wù)響應(yīng)的模塊,該模塊構(gòu)建和啟用依賴(lài)zlib庫(kù)豺憔。 -
--without-http_rewrite_module
—— 禁止編譯允許http服務(wù)重定向請(qǐng)求和更改請(qǐng)求URI的模塊额获,該模塊構(gòu)建和啟用依賴(lài)pcre庫(kù)。 -
--without-http_proxy_module
—— 禁止編譯http服務(wù)代理模塊恭应。 -
--with-http_ssl_module
—— 啟用編譯一個(gè)支持把https加到http服務(wù)的模塊抄邀。該模塊默認(rèn)不編譯,依賴(lài)OpenSSL庫(kù)昼榛。 -
--with-pcre=path
—— 設(shè)置pcre庫(kù)源碼路徑境肾。該庫(kù)是正則表達(dá)式所必須的,推薦安裝胆屿。 -
--with-pcre-jit
—— 用即時(shí)編譯構(gòu)建pcre庫(kù)奥喻。 -
--with-zlib=path
—— 設(shè)置zlib庫(kù)源碼路徑。該庫(kù)是壓縮模塊所必須的非迹,推薦安裝环鲤。 -
--with-cc-opt=parameters
—— 該選項(xiàng)設(shè)置的參數(shù)將被添加到CFLAGS變量,在FreeBSD系統(tǒng)中使用系統(tǒng)pcre庫(kù)時(shí)憎兽,--with-cc-opt="-I /usr/local/include"
將被指定楔绞。如果select()支持的文件數(shù)量需要增加,該參數(shù)可指定如下--with-cc-opt="-D FD_SETSIZE=2048"
唇兑。 -
--with-ld-opt=parameters
—— 該選項(xiàng)設(shè)置的參數(shù)將在鏈接時(shí)使用酒朵。在FreeBSD系統(tǒng)中使用系統(tǒng)pcre庫(kù)時(shí),指定--with-ld-opt="-L /usr/local/lib"
扎附。
4蔫耽、常用配置
# 啟動(dòng)nginx的用戶(hù)名和組名
user develope develope;
# 工作線程數(shù)量,此配置最好為cpu核心數(shù)量或者2倍
worker_processes 4;
events {
# worker_connections 指定最大可以同時(shí)接收的連接數(shù)量,這里一定要注意匙铡,最大連接數(shù)量是和worker processes共同決定的图甜。
worker_connections 4096;
# 配置指定nginx在收到一個(gè)新連接通知后盡可能多的接受更多的連接
multi_accept on;
# 如果是linux2.6+,使用epoll鳖眼,如果是BSD如Mac請(qǐng)使用Kqueue
use epoll;
}
# 核心配置 配置虛擬主機(jī)集合
http {
include mime.types;
default_type application/octet-stream;
# 日志格式定義
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
# http請(qǐng)求包最大數(shù)據(jù) 30m
client_max_body_size 30m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
types_hash_max_size 2048;
# 設(shè)定請(qǐng)求緩沖
client_header_buffer_size 128k; # 指定客戶(hù)端請(qǐng)求頭緩存大小黑毅,當(dāng)請(qǐng)求頭大于 1KB 時(shí)會(huì)用到該項(xiàng)
large_client_header_buffers 4 128k; # 最大數(shù)量和最大客戶(hù)端請(qǐng)求頭的大小
##
# # SSL Settings
#
#
##配置共享會(huì)話緩存大小
ssl_session_cache shared:SSL:10m;
# 配置會(huì)話超時(shí)時(shí)間
ssl_session_timeout 10m;
##
# # Logging Settings
# ##
#
access_log /usr/local/lib/logs/nginx/http-access.log; ## 訪問(wèn)日志
error_log /usr/local/lib/logs/nginx/http-error.log; ## 錯(cuò)誤日志
# 啟用gzip
gzip on;
gzip_disable "msie [1-6]";
# 負(fù)載均衡配置
upstream service.upstream.com {
# 輪循模式
# weight 權(quán)重
# ip_hash 通過(guò)ip的hash值
# backup 其它所有非backup都down的時(shí)候啟用backup
# fair 按后端服務(wù)器的響應(yīng)時(shí)間來(lái)分配請(qǐng)求,響應(yīng)時(shí)間短的優(yōu)先分配钦讳。與weight分配策略類(lèi)似矿瘦。
# down 不參與
server 172.26.203.175:8020 weight=10; # ZZ-APP-SERVER-003
server 172.26.203.174:8020 weight=10; # ZZ-APP-SERVER-002
server 172.26.203.172:8020 weight=10; # ZZ-APP-SERVER-001
}
# 定義綁定主機(jī)
server {
# 監(jiān)聽(tīng)端口
listen 80;
# 此主機(jī)監(jiān)聽(tīng)host_name
server_name www.xxx.com; # API service
# 日志
access_log /usr/local/lib/logs/nginx/host.service.log main;
# location 配置可以有多個(gè) 遵守最長(zhǎng)匹配原則
# 普通配置
location / {
root /usr/local/lib/www;
index index.html index.htm;
}
# 轉(zhuǎn)發(fā)配置
location /api/ {
proxy_pass http://service.upstream.com;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 特殊處理
if ($host = "www.xxx.com") {
return 301 https://$server_name$request_uri;
}
}
# https
server {
listen 443 ssl;
server_name www.xxx.com;
#設(shè)置長(zhǎng)連接
keepalive_timeout 70;
# 證書(shū)地址
ssl_certificate /usr/local/nginx/cert/www.xxx.com.pem;
ssl_certificate_key /usr/local/nginx/cert/www.xxx.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
#優(yōu)先采取服務(wù)器算法
ssl_prefer_server_ciphers on;
# HSTS策略
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;preload" always;
#防XSS攻擊
add_header X-Xss-Protection 1;
location / {
proxy_pass http://service.upstream.com;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 引入其它配置
include /usr/local/lib/web-http.conf;
}
區(qū)別手機(jī)端
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
{
root /usr/local/website/mobile;
}