一、切換目錄javascript
# cd /usr/local/srcphp
二、下載css
# wget http://nginx.org/download/nginx-1.12.1.tar.gzhtml
三、解壓java
# tar xzvf nginx-1.12.1.tar.gznode
四、切換到nginx目錄下linux
# cd nginx-1.12.1/nginx
五、編譯git
# ./configure --prefix=/usr/local/nginxapache
以上編譯參數是通常狀況下使用,在實際編譯時,須要根據需求添加參數,好比網站添加了ssl證書時,就須要將ssl相關的模塊給nginx配置上,所以咱們須要注意後續將下載的源碼包保留,可能後續會用到裏面包含的某個模塊
六、make && make install
# make && make install
安裝完成後,咱們來看下/usr/local/nginx目錄下的文件
其中conf下是nginx的配置文件
html下是樣例文件
logs目錄存放日誌文件,sbin下面是進程,也就是核心文件
支持 -t檢查配置文件語法
七、編輯啓動腳本
啓動腳本須要放在/etc/init.d/目錄下
# vim /etc/init.d/nginx //建立並編輯啓動腳本
在啓動腳本中加入如下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx):
#!/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
八、修改啓動腳本權限
# chmod 755 /etc/init.d/nginx
九、將nginx加入啓動服務列表
# chkconfig --add nginx
十、編輯配置文件
此時/usr/local/nginx/conf/目錄下已經有一個nginx.conf配置文件,
可是咱們不用這個配置文件,咱們用本身寫的配置文件,全部咱們先將原來的nginx.conf備份
# mv nginx.conf nginx.conf.bak
接下來建立並編輯咱們本身的nginx.conf配置文件
# vim nginx.conf
在配置文件中加入如下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf):
user nobody nobody; //user用於定義啓動nginx的是哪一個用戶,也就是若是要讓nginx去作讀寫操做時是使用誰的身份去操做的
worker_processes 2; //定義子進程有幾個
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200; //定義nginx最多能夠打開多少個文件
events
{
use epoll; //使用epoll模式
worker_connections 6000; //進程最大鏈接數
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server //每一個server對應一個虛擬主機
{
listen 80; //nginx監聽的端口
server_name localhost; //網站域名
index index.html index.htm index.php;
root /usr/local/nginx/html; //網站的根目錄
location ~ \.php$ //用於配置解析php的
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; //指定php-fpm監聽的端口或socket,可替代爲fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
十一、檢查配置文件語法錯誤
# /usr/local/nginx/sbin/nginx -t
十二、啓動
# /etc/init.d/nginx start //啓動前應確認apache服務已經關閉,若是沒有關閉,出現以下現象
1三、查看nginx是否成功啓動
# ps aux |grep nginx //下圖中Ss表示此進程爲父進程,下面爲2個worker process子進程,通常父進程屬主都是root,子進程屬主爲上面咱們配置的nobody
# ps aux |grep php-fpm //一樣,php-fpm父進程屬主都是root,子進程屬主爲上面咱們配置的php-fpm
1四、測試訪問默認頁面
使用下面這兩組命令訪問的結果是同樣的
# curl localhost
# curl 127.0.0.1:80
上圖中訪問到的頁面實際上就是咱們在配置文件中配置的第一個虛擬主機(也是默認虛擬主機)設置的默認訪問頁面/usr/local/nginx/html/index.html
進一步測試,咱們在/usr/local/nginx/html/目錄下寫一個1.php的文件,看是否能成功解析,1.php中寫入如下內容:
<?php
echo "This is nginx test page!";
?>
保存後,咱們來訪問這個頁面
# curl localhost/1.php //下面是訪問成功截圖
一、修改配置文件
去掉下圖框中內容:
在倒數第二行增長以下內容:
include vhost/*.conf; //表示虛擬主機配置文件放在當前目錄下的子目錄vhost中;
二、建立虛擬主機配置文件存放目錄
# mkdir vhost //當前目錄爲/usr/local/nginx/conf/
三、建立並編輯虛擬主機配置文件
# vim vhost/aaa.com.conf
在文件中寫入如下內容:
server
{
listen 80 default_server; // 有default_server這個標記的就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php; //指定索引頁
root /data/wwwroot/default; //網站所在目錄
}
四、建立網站根目錄
若是網站指定的目錄還不存在的話,須要建立這個目錄
# mkdir /data/wwwroot/default
五、在網站目錄下寫一個頁面
# cd /data/wwwroot/default
# vim index.html
文件中寫入如下內容:
六、測試nginx配置文件是否有語法錯誤
# /usr/local/nginx/sbin/nginx -t
七、從新加載配置文件
# /etc/init.d/nginx restart //此命令直接經過重啓nginx來從新加載配置文件
# /usr/local/nginx/sbin/nginx -s reload //此命令不用重啓nginx服務就能夠從新加載配置文件
八、測試訪問咱們寫的頁面
# curl localhost
# curl -x127.0.0.1:80 bbb.com //目前是沒有這個網站的,所以咱們訪問到的是默認的虛擬主機
八、如何區分默認虛擬主機和非默認虛擬主機
nginx找server的時候確定會從第一個開始,所以在沒有設定默認虛擬主機時,nginx會自動將排在第一個的server做爲默認虛擬主機
另外就是找帶有default_server標記的server即爲默認虛擬主機
一、建立一個測試用的虛擬主機
首先切換到vhost虛擬主機配置文件目錄下,新增一個虛擬主機配置文件
# test.com.conf
在這個文件裏寫入如下內容:
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic "Auth"; //用於定義用戶認證的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用戶名密碼文件
}
}
二、生成密碼文件
使用apache2.4自帶的htpsswd命令或文件來生成密碼文件,若是沒有安裝apache,可使用yum命令安裝一個
# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lijie
其中-c指建立,/usr/local/nginx/conf/htpasswd是存放路徑 ,lijie是用戶名
按照上圖提示輸入兩次密碼後,咱們再來查看密碼文件內容
接下來再來建立一個用戶就不用加-c選項了
三、測試語法並從新加載
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
四、訪問這個測試虛擬主機
# curl -x127.0.0.1:80 test.com -I //提示401錯誤,須要用戶認證
接下來咱們輸入用戶名和密碼來訪問
# curl -ulj:112233 -x127.0.0.1:80 test.com //其中lj是用戶名,112233是密碼
咱們看到上圖提示404錯誤,這是由於咱們以前沒有建立訪問的目錄及索引頁,如今咱們來建立一個
這時咱們再來訪問就能夠看到index.html中的內容了
五、針對目錄的用戶認證
咱們只須要在虛擬主機配置文件/usr/local/nginx/conf/vhost/test.com.conf中做出修改便可
修改前的配置文件內容
修改後的配置文件內容以下,表示是針對admin這個目錄進行用戶認證
修改後,訪問test.com就不須要作用戶認證,只有訪問test.com/admin時才須要作用戶認證
六、針對某個頁面的用戶認證
訪問某個頁面才須要用戶認證的話,須要將配置文件/usr/local/nginx/conf/vhost/test.com.conf修改成以下內容:
上圖中~表示匹配,~ admin.php表示匹配到admin.php這個頁面就須要作用戶認證
一、修改配置文件
當前咱們已經有test.com這個域名來訪問test.com這個網站,爲了使另外兩個域名test2.com和test3.com也跳轉到test.com這個網站,咱們須要修改配置文件
# vim /usr/local/nginx/conf/vhost/test.com.conf
如下內容爲域名重定向使用的代碼
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
修改前
修改後,下圖框中部分爲新增內容:
上面^/(.*)$是http://$host/(.*)$的簡寫,^表示的就是域名,(.*)$表明的是域名後面跟着的一長串,$1表明的就是(.*)
permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302
二、檢查語法並從新加載
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
三、測試驗證域名重定向
驗證訪問test2.com,能夠看到狀態碼301重定向到test.com
驗證訪問test2.com/wrwer/err,能夠看到狀態碼301重定向到test.com/wrwer/err
驗證訪問test3.com/wrwer/err,能夠看到狀態碼301重定向到test.com/wrwer/err
驗證訪問test4.com/wrwer/err,提示頁面未找到,其實是跳轉到了默認虛擬主機aaa.com去了。