一、nginx安裝過程
rpm -qa gcc-c++
rpm -qa pcre pcre-devel###(nginx的URL重寫rewrite模塊用到,pcre就用來幹這個)
rpm -qa openssl openssl-devel(加密的 https443端口是加密的http服務)
yum install openssl openssl-devel -y(https加密的時候要用到)
useradd nginx -s /sbin/nologin -M
cd /tools/nginx-1.9.2
./configure --prefix=/application/nginx-1.9.2/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module######(--prefix=PATH 設置安裝路徑
--user=USER進程用戶權限 --group=GROUP進程用戶組權限 --with-http_stub_status_module激活狀態信息--with-http_ssl_module 激活ssl功能)
make
make install
/application/nginx-1.9.2/sbin/nginx -t 檢查語法
查看參數 /application/nginx/sbin/nginx -V
有錯誤可以去/application/nginx/logs/error.log查看
詳解nginx.conf
cd /application/nginx/conf/
egrep -v "#|^$" nginx.conf.default後查看有用信息
worker_processes 1;##worker進程數量
(error_log logs/error.log) error;##日誌在application/nginx/logs/error.log#相對路徑默認存在
events {##時間區塊開始
worker_connections 1024;##每個worker進程支持最大的連接數
}
http {##http區塊開始
include mime.types;##nginx支持的媒體類型庫文件包含
default_type application/octet-stream;##默認的媒體類型
sendfile on;##開啓高效傳輸模式
keepalive_timeout 65;##連接超時
server {##第一個server區塊開始,表示一個獨立的虛擬主機站點
listen 80;##提供服務的端口,默認8當
server_name localhost;##提供服務的域名主機名可加別名
location / {##第一個location區塊開始
root html/www;##站點的根目錄,相對於nginx安裝目錄
index index.html index htm;##默認的首頁文件,多個用空格分開
}#第一個location區塊結果
error_page 500 502 503 504 /50x.html;##出現對應的http狀態碼試用50x.html迴應
location = /50x.html {##location區塊開始,訪問50x.html,
root html;
}
}
}
二、搭建基於域名的主機實戰
1/配置nginx.conf
cd /application/nginx/conf/
egrep -v "^$|#" nginx.conf.defult >nginx.conf
vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.liangtiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
}
mkdir /application/nginx/html/www -p
echo "www.liangtiantian====>>192.168.88.90" >html/www/index.html
2、檢查語法,重新加載nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
3、配置hosts。測試
vim /etc/hosts
192.168.88.89 www.etiantian.org bbs.etiantian.org
curl www.etiantian.org
windows:
在C:\Windows\System32\drivers\etc\hosts
加入192.168.88.89 www.etiantian.org bbs.etiantian.org
瀏覽器裏訪問www.etiantian.org
拓展:
a、基於端口的虛擬主機
server {
listen 8888;
server_name www.liangtiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
/application/nginx/sbin/nginx -t 檢測語法
/application/nginx/sbin/nginx -s reload 平滑加載
http://www.liangtiantian.org:8888
b、優化
1.include分割各個模塊
在http模塊
http {##http區塊開始
include mime.types;##nginx支持的媒體類型庫文件包含
default_type application/octet-stream;##默認的媒體類型
sendfile on;##開啓高效傳輸模式
keepalive_timeout; 65;##連接超時
include extra/*.conf;###文件放在conf下面
}
mkdir extra
cp nginx.conf nginx.conf.base-name
sed -n "11,18p" nginx.conf >extra/www.conf
sed -n "20,27p" nginx.conf >extra/blog.conf
sed -i "10,44d" nginx.conf
sed -i '10 i include extra/www.conf;\ninclude extra/blog.conf;' nginx.conf
../sbin/nginx -s reload
2.Nginx status模塊配置
作用:提供服務狀態信息
模塊:http_stub_status_module
cat >>/application/nginx/conf/extra/status.conf<<EOF
##status
server{
listen 80;
server_name status.etiantian.org;
location /{
stub_status on;
access_log off;
allow 10.0.0.0/24;
deny all;
}
}
EOF
C:\Windows\System32\drivers\etc\hosts 文件更改解析
主機解析不要忘記
查看status.etiantian.org
3.增加錯誤日誌(error_log)
error_log file level;
關鍵字 日誌文件 錯誤日誌級別
4.rewrite301跳轉
需要pcre支持
在http中添加server新模塊
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}