編譯安裝nginx
1.安裝Nginx所需pcre庫
????pcre全程Perl Compatible Regular Expressions,中文perl兼容正則表達(dá)式,官方網(wǎng)址:http://www.pcre.org/,安裝pcre庫是為了使Nginx支持HTTP Rewrite模塊。安裝過程如下:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar xzvf pcre-8.40.tar.gz
cd pcre-8.40/
./configure
make && make install
2.編譯openssl
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar xzf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l
./config
make && make install
3.安裝Nginx
wget http://nginx.org/download/nginx-1.12.1.tar.gz
# 添加nginx用戶,-s /sbin/nologin 禁止登錄 -M 不產(chǎn)生home目錄
useradd nginx -s /sbin/nologi -M
tar xzvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.2l(若編譯出現(xiàn)ssl相關(guān)錯誤,加上此選項(xiàng)重新編譯,此目錄是openssl的源碼目錄,可根據(jù)實(shí)際情況自行修改)
make && make install
ln -s /application/nginx-1.12.1 /application/nginx
# 檢查nginx配置是否有錯誤
/application/nginx/sbin/nginx -t
# 若出現(xiàn)找不到某些共享庫的情況,通過find找到該庫的路徑目錄,然后加入到/etc/ld.so.conf,然后執(zhí)行l(wèi)dconfig命令使其生效
# 若語法檢查沒有錯誤,可以直接啟動nginx
/application/nginx/sbin/nginx
/application/nginx/sbin/nginx -s reload # 平滑重啟
4.排查問題
????此時訪問80端口,會出現(xiàn)Welcome to Nginx的頁面,若沒有成功,可以從以下幾個方面檢查:
4.1.iptables防火墻和selinux是否關(guān)閉。
# 關(guān)閉防火墻
/etc/init.d/iptables stop
# 若允許外網(wǎng)ip訪問80端口,則不能關(guān)閉,而是將防火墻80端口放開,命令:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# 非正式環(huán)境可以禁止防火墻開啟啟動,便于學(xué)習(xí)調(diào)試Nginx服務(wù),具體請學(xué)習(xí)防火墻技術(shù)
chkconfig iptables off
# 查看防火墻狀態(tài)
/etc/init.d/iptables status
4.2.關(guān)閉selinux
# 臨時關(guān)閉selinux
setenforce 0
# 永久關(guān)閉,編輯/etc/selinux/config,配置如下選項(xiàng),并重啟生效
SELINUX=disable
4.3.檢查日志
nginx默認(rèn)日志文件存放在啟動目錄的logs文件夾下,查看日志獲取報錯信息,解決問題
windows中C:\Windows\System32\drivers\etc\hosts用于電腦解析DNS配置
二.Nginx配置
1.Nginx的配置文件
????nginx配置文件放置在安裝目錄的conf目錄下,其中nginx.conf為其主要配置文件,相當(dāng)于apache的httpd.conf文件,配置文件的每行都以;結(jié)尾。
user nginx nginx; # 配置nginx服務(wù)的用戶及組
worker_processes 1; # 配置啟動進(jìn)程的個數(shù),一般為cpu個數(shù)的2倍
error_log /application/logs/nginx_error.log crit; # 錯誤日志的配置及日志級別
pid logs/nginx.pid; # 配置存放nginx進(jìn)程ID的文件,方便后續(xù)發(fā)送信號
events {
use epoll; # 使用epoll
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"';
server { # 需要包含在http {} 中用于虛擬主機(jī)的配置
listen 80;
server_name www.flyingdown.com flyingdown.com; # 配置域名及別名
location / {
root html; # 配置該虛擬主機(jī)默認(rèn)站點(diǎn)目錄,本例相當(dāng)于/application/nginx/html目錄
index index.html index.htm; # 配置默認(rèn)頁面
# 配置訪問日志
access_log /application/logs/www_access.log main
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.優(yōu)化vhosts配置
????主配置文件可以包含虛擬主機(jī)配置,但當(dāng)虛擬主機(jī)很多時,會使主配置文件看起來很亂,不便于維護(hù),我們可以使用include語法,將虛擬主機(jī)分離出來。
????主配置文件
user nginx nginx;
worker_processes 2;
error_log /app/logs/nginx_err.log crit;
events {
use epoll;
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 extra/nginx_vhosts.conf;
}
????然后新建extra目錄,新建nginx_vhosts.conf,寫入一下內(nèi)容
server {
listen 80;
server_name www.flydowning.com flydowning.com;
location / {
root html/base;
index index.html index.htm;
access_log /app/logs/www_access.log main;
}
location ~.*/static/ {
root html;
access_log /app/logs/couplet_access.log main;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name couplet.flydowning.com;
location / {
root html/couplet;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
????甚至,我們可以將每一個虛擬主機(jī)的配置放置一個單獨(dú)的文件,然后使用include語法,包含至主配置文件中,這里不做展示
3.配置狀態(tài)信息虛擬主機(jī)
????如果我們想在nginx運(yùn)行時動態(tài)的觀察nginx運(yùn)行的狀態(tài)、連接數(shù)等信息,我們可以添加狀態(tài)信息虛擬主機(jī)的配置,如下:
server {
listen 80;
server_name status.flyingdown.com;
location / {
stub_status on;
access_log off;
}
}
????在hosts文件中添加status.flyingdown.com的映射,然后訪問,可以看到如下內(nèi)容:
????4.基于端口的虛擬主機(jī)配置
????修改虛擬主機(jī)的listen端口即可
5.基于IP的虛擬主機(jī)配置
????將server_name修改為對應(yīng)的IP地址,同時將listen監(jiān)聽添加上ip:port形式
6.配置多個nginx實(shí)例
????Nginx多實(shí)例命令參數(shù),我們可以通過安裝后的/application/nginx/sbin/nginx加上-h參數(shù)查看相關(guān)用法:
[-root-@/root]#/application/nginx/sbin/nginx -h
nginx version: nginx/1.12.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /application/nginx-1.12.1/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
????可以使用-c參數(shù)指定其他配置文件啟動多個nginx實(shí)例