首先安裝必要的庫(nginx 中g(shù)zip模塊需要 zlib 庫,rewrite模塊需要 pcre 庫,ssl 功能需要openssl庫)
(配置ssl證書出錯(cuò))https://blog.csdn.net/weixin_38111957/article/details/81283121
安裝make:
yum -y install gcc automake autoconf libtool make
安裝g++:
yum install gcc gcc-c++
安裝PCRE庫:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure
make && make install
安裝zlib庫:
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
安裝openssl(某些vps默認(rèn)沒裝ssl):
查看是否安裝openssl:
openssl version
如果已安裝就跳過
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
./configure
make && make install
安裝nginx:
wget http://nginx.org/download/nginx-1.15.9.tar.gz
./configure
make && make install
配置
在/etc/init.d/目錄下,創(chuàng)建nginx啟動(dòng)腳本,并寫入內(nèi)容:
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart() {
stop
start
}
configtest() {
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
給nginx(啟動(dòng)腳本) 設(shè)定755權(quán)限:
chmod 755 /etc/init.d/nginx
添加 nginx服務(wù) 到服務(wù)列表:
chkconfig --add nginx
設(shè)定 nginx服務(wù) 開機(jī)啟動(dòng):
chkconfig nginx on
修改配置文件:
vim /usr/local/nginx/conf/nginx.conf
再http{}里面的最下面添加
include /usr/local/nginx/vhost/*.conf;
檢測(cè)nginx配置文件是否有錯(cuò)
/usr/local/nginx/sbin/nginx -t
啟動(dòng)nginx服務(wù)
/etc/init.d/nginx start
在vhost里面添加admin.conf和www.conf
erver {
listen 80;
server_name admin.alipayjf.com;
root /data/www/admin;
location / {
try_files $uri $uri/ /index.html;
}
}
erver {
listen 80;
server_name www.alipayjf.com;
location / {
proxy_pass http://127.0.0.1:8688;
}
location /html {
alias /data/www/html/;
}
}
配置說明
Nginx配置文件常見結(jié)構(gòu)的從外到內(nèi)依次是「http」「server」「location」等等,缺省的繼承關(guān)系是從外到內(nèi),也就是說內(nèi)層塊會(huì)自動(dòng)獲取外層塊的值作為缺省值。
Server
接收請(qǐng)求的服務(wù)器需要將不同的請(qǐng)求按規(guī)則轉(zhuǎn)發(fā)到不同的后端服務(wù)器上,在 nginx 中我們可以通過構(gòu)建虛擬主機(jī)(server)的概念來將這些不同的服務(wù)配置隔離。
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
}
這里的 listen 指監(jiān)聽端口,server_name 用來指定IP或域名,多個(gè)域名對(duì)應(yīng)統(tǒng)一規(guī)則可以空格分開,index 用于設(shè)定訪問的默認(rèn)首頁地址,root 指令用于指定虛擬主機(jī)的網(wǎng)頁跟目錄,這個(gè)地方可以是相對(duì)地址也可以是絕對(duì)地址。
Localtion
每個(gè) url 請(qǐng)求都會(huì)對(duì)應(yīng)的一個(gè)服務(wù),nginx 進(jìn)行處理轉(zhuǎn)發(fā)或者是本地的一個(gè)文件路徑,或者是其他服務(wù)器的一個(gè)服務(wù)路徑。而這個(gè)路徑的匹配是通過 location 來進(jìn)行的。我們可以將 server 當(dāng)做對(duì)應(yīng)一個(gè)域名進(jìn)行的配置,而 location 是在一個(gè)域名下對(duì)更精細(xì)的路徑進(jìn)行配置。
以上面的例子,可以將root和index指令放到一個(gè)location中,那么只有在匹配到這個(gè)location時(shí)才會(huì)訪問root后的內(nèi)容:
location / {
root /data/www/host2;
index index.html index.htm;
}
靜態(tài)文件映射
訪問文件的配置主要有 root 和 aliasp's 兩個(gè)指令。這兩個(gè)指令的區(qū)別容易弄混:
alias后跟的指定目錄是準(zhǔn)確的,并且末尾必須加 /。
location /c/ {
alias /a/;
}
如果訪問站點(diǎn)http://location/c訪問的就是/a/目錄下的站點(diǎn)信息。
root后跟的指定目錄是上級(jí)目錄,并且該上級(jí)目錄下要含有和location后指定名稱的同名目錄才行。
location /c/ {
root /a/;
}
這時(shí)訪問站點(diǎn)http://location/c訪問的就是/a/c目錄下的站點(diǎn)信息。
如果你需要將這個(gè)目錄展開,在這個(gè)location的末尾加上「autoindex on; 」就可以了
轉(zhuǎn)發(fā)
配置起來很簡(jiǎn)單比如我要將所有的請(qǐng)求到轉(zhuǎn)移到真正提供服務(wù)的一臺(tái)機(jī)器的 8001 端口,只要這樣:
location / {
proxy_pass 172.16.1.1:8001;
}
這樣訪問host時(shí),就都被轉(zhuǎn)發(fā)到 172.16.1.1的8001端口去了。
負(fù)載均衡
upstream myserver; {
ip_hash;
server 172.16.1.1:8001;
server 172.16.1.2:8002;
server 172.16.1.3;
server 172.16.1.4;
}
location / {
proxy_pass http://myserver;
}
我們?cè)?upstream 中指定了一組機(jī)器,并將這個(gè)組命名為 myserver,這樣在 proxypass 中只要將請(qǐng)求轉(zhuǎn)移到 myserver 這個(gè) upstream 中我們就實(shí)現(xiàn)了在四臺(tái)機(jī)器的反向代理加負(fù)載均衡。其中的 ip_hash 指明了我們均衡的方式是按照用戶的 ip 地址進(jìn)行分配。另外還有輪詢、指定權(quán)重輪詢、fair、url_hash幾種調(diào)度算法