Nginx是一款很是優秀的web服務軟件,不但能夠作web服務軟件,還能夠作反向代理負載均衡和前端業務的緩存服務php
[root@jiangjunwang ~]# yum install -y pcre-devel openssl-devel
說明:pcre-devel: perl語言正則表達式兼容軟件包、openssl-devel:使系統支持https方式訪問css
[root@jiangjunwang ~]# useradd www -s /sbin/nologin/ -Mhtml
[root@jiangjunwang ~]# mkdir /server/tools -p
[root@jiangjunwang ~]# cd /server/tools/
[root@jiangjunwang tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@jiangjunwang tools]# tar xf nginx-1.12.2.tar.gz 前端
[root@jiangjunwang nginx-1.12.2]# ./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
說明:java
[root@jiangjunwang nginx-1.12.2]# make && make install nginx
[root@jiangjunwang nginx-1.12.2]# ln -s /application/nginx-12.2 /application/nginxweb
[root@jiangjunwang nginx-1.12.2]# /application/nginx/sbin/nginx正則表達式
出現以上界面表示nginx安裝成功建議使用谷歌瀏覽器進行訪問 apache
簡化配置文件
[root@jiangjunwang conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
b
sed -n '10,21p' nginx.conf>../conf/extra/www.conf
編輯主配置文件vim
[root@jiangjunwang conf]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
}
編輯虛擬機配置文件
[root@jiangjunwang html]# vim /application/nginx/conf/extra/bbs.conf
server {
listen 80;
server_name bbs.av.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}[root@jiangjunwang html]# vim /application/nginx/conf/extra/www.conf
server {
listen 80;
server_name www.av.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
編寫測試index.html文件
[root@jiangjunwang html]# echo "bbs.av.org">/application/nginx/html/bbs/index.html
[root@jiangjunwang html]# echo "www.av.org">/application/nginx/html/www/index.html
檢查配置文件語法並重啓服務
[root@jiangjunwang html]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-12.2/conf/nginx.conf test is successful
[root@jiangjunwang html]# /application/nginx/sbin/nginx -s reload
**編寫本機hosts文件並測試
看到如上效果表示一個基於域名的nginx虛擬主機配置完成
所謂主機別名就是讓一個IP地址對應多個域名主機,這也是在實際應用中經常使用的功能
配置方法接着上邊的配置咱們以bbs.av.com站點爲例配置一個別名爲bbs.a.com
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
而後重啓nginx進行訪問測試便可
[root@jiangjunwang conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
}
編輯主配置文件先定義一個log_format日誌格式的參數
[root@jiangjunwang conf]# vim nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
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/www.conf;
include extra/bbs.conf;
}
而後編輯bbs站點目錄文件使其使用主配置文件定義的格式生成相應站點的訪問日誌
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
最後檢查語法重啓nginx服務便可
在實際生產環境中access日誌會逐漸變大,等大到必定程度的時候會打不開這個日誌文件,因此須要咱們按期的對日誌文件進行切割
[root@jiangjunwang logs]# vim /server/scripts/cut_access.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/bbs.access.log /application/nginx/logs/access.log.$data_info
/application/nginx/sbin/nginx -s reload#cut nginx log cron
00 00 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
location 指令的做用是根據用戶請求的uri來執行不一樣的應用或者炒做
location基本的語法爲
= --- 精確匹配網站uri資源信息
~ --- 區分大小寫匹配網站uri資源信息
~* --- 不區分大小寫匹配網站uri資源信息
^~ --- 優先匹配網站uri資源信息
/AV/ --- 指定匹配網站資源目錄信息
/ --- 默認匹配網站資源信息
! --- 對匹配的內容進行取反
一個栗子
需求:內網用戶能夠訪問www站點AV目錄而外網用戶不能訪問
編寫www站點文件
[root@jiangjunwang extra]# vim www.conf
server {
listen 80;
server_name www.av.org;
root html/www;
index index.html index.htm;
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
}
}
[root@jiangjunwang www]# echo "AV info" >AV/oldboy.html
檢查語法重啓nginx服務分別用內網172網段和外網10網段訪問測試便可
Nginx rewrite主要的功能就是實現URL地址重寫,Nginx rewrite須要pcre軟件的支持,經過prel兼容的正則表達式語法進行規則匹配;
一個栗子
經過rewrite模塊實現訪問av.org自動跳轉到bbs.av.org
編輯bbs站點配置文件
方法一
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name av.org;
root html/bbs;
index index.html index.html;
rewrite ^/(.*) http://bbs.av.org/$1 permanent;}
server {
listen 80;server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;}
}
而後檢查語法重啓nginx服務便可
方法二
經過location區塊和rewrite結合實現
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;server_name bbs.av.org bbs.a.com;
location / {
root html/bbs;
index index.html index.htm;
if ($host ~ "^av.org$") {
rewrite ^/(.) http://bbs.bbs.org/$1 permanent;
}
access_log logs/bbs.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;}}
}
~
而後檢查語法重啓nginx服務便可
有的時候在實際工做工做中企業要求咱們爲網站設置訪問密碼權限,這樣操做後只有擁有帳號密碼的用戶才能夠訪問網站,通常在企業內部使用
這裏咱們一前面配置的bbs網站爲例進行說明
[root@jiangjunwang extra]# yum -y install httpd
[root@jiangjunwang extra]# htpasswd -bc /application/nginx/conf/htpasswd jiang 123456
[root@jiangjunwang extra]# chmod 400 /application/nginx/conf/htpasswd
[root@jiangjunwang extra]# chown www /application/nginx/conf/htpasswd
[root@jiangjunwang extra]# vim bbs.conf
server {
listen 80;
server_name av.org;
rewrite ^/(.*) http://bbs.av.org/$1 permanent;
}server {
listen 80;server_name bbs.av.org;
location / {
root html/bbs;
index index.html index.htm;
auth_basic "jun wang";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
}
說明:auth_basic 「jun wang」 這一行爲驗證的提示符;auth_basic_user_file 後邊接認證密碼文件;
看到以下界面表示認證成功