LNMP第二部分nginx、php配置(用戶認證、域名重定向、日誌、配置緩存、防盜鏈)javascript
一.nginx的配置( nginx.conf) php
1、nginx的主配置文件位置: /usr/local/nginx/conf/nginx.concss
2、清空 /usr/local/nginx/conf/nginx.con默認的配置文件內容html
[root@mysql ~]# > /usr/local/nginx/conf/nginx.confjava
>:重定向的意思,單獨使用,能夠把一個文本文檔快速清空node
3、拷貝一下代碼到/usr/local/nginx/conf/nginx.conf文件中,虛擬主機和主配置文件寫在一塊兒mysql
主配置文件內容以下:linux
user nobody nobody;nginx
worker_processes 2; #開啓幾個子線程web
error_log /usr/local/nginx/logs/nginx_error.log crit; #錯誤日誌級別,crit是很是嚴謹的級別
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200; #單個進程最大鏈接數
events
{
use epoll;
worker_connections 6000; #這裏的數字不易過大
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526; #虛擬主機多,若是這個值設置的很小的話可能會致使沒法啓動,有三四域名,那麼設置成256基本就能夠
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
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
user nobody nobody:user後面跟的是nginx運行的用戶和組
1-1024端口必需要用超級用戶才能監聽,普通用戶是沒法監聽的
4、也可使用另一種寫法,就是把虛擬主機的配置文件和主配置文件單獨寫,而不是寫在主配置文件中
1)主配置文件寫法
user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use 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; include vhosts/*.conf; #這裏也能夠寫成絕對路徑 }
2)配置默認的虛擬機,這是第一個虛擬機也是默認的虛擬機,就是在沒有指定的狀況下,都會跳轉到這裏
① 在/usr/local/nginx/conf/目錄下建立vhosts目錄
[root@mysql ~]# mkdir /usr/local/nginx/conf/vhosts
②進入/usr/local/nginx/conf/vhosts目錄
[root@mysql ~]#cd /usr/local/nginx/conf/vhosts/
③在使用vm建立一個default.cong文件,而且拷貝一下代碼到default.conf文件中
[root@mysql vhosts]# vim default.conf
虛擬機配置文件代碼以下:
server { listen 80; #在80後面加上default就變成默認的虛擬主機了 server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; #若是監聽的是127.0.0.1:9000的端口,那麼這裏就要寫上127.0.0.1:9000 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
如圖所示:
④而後退出保存
⑤檢查看看有沒有錯誤
[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
出現這個提示就表示配置文件沒有錯誤
⑥從新加載:[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload
⑦如何檢查配置文件有沒有加載:
須要作的就是故意把/usr/local/nginx/conf/vhosts/配置文件寫錯,而後使用/usr/local/nginx/sbin/nginx -t檢查配置文件看看是否有報錯,若是沒有,那就說明配置文件沒有被加載,若是有說嗎配置未見出錯,那麼就證實配置文件已經被加載
3)配置第二個虛擬機
①進入到/usr/local/nginx/conf/vhosts目錄下,而後拷貝第一個虛擬主機的配置文件到當前目錄下,而且重命名
[root@mysql vhosts]#cp default.conf default2.conf
②修改default2.conf文件,具體以下
代碼以下:
server { listen 80; #端口號也是能夠修改的 server_name www.guhantai.com.cn www.guhantai.cn; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
③退出保存
④檢查配置文件是否有錯誤
[root@mysql vhosts]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
⑤從新加載
[root@mysql vhosts]#/usr/local/nginx/sbin/nginx -s reload
⑥測試
default: 表示有一個默認的虛擬機主機
二. php-fpm.conf
1、清空/usr/local/php/etc/php-fpm.conf文件中的內容,而後寫入以下配置:
vim /usr/local/php/etc/php-fpm.conf
配置文件內容:
[global] #全局配置
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log #定義錯誤日誌位置
[www]
listen = /tmp/php-fcgi.sock #或者寫成127.0.0.1:80也能夠
user = php-fpm
group = php-fpm
listen.owner = nobody //和後面的nginx的一致
listen.group = nobody // 同上
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5 #最小的攀升資源數
pm.max_spare_servers = 35 #最大的攀升資源數
pm.max_requests = 500
rlimit_files = 1024
如圖所示:
有多個池子的寫法以下圖:
listen.owner = nobody //和後面的nginx的一致,若是不定義,默認會以root的身份去生成這個/tmp/php-fcgi.sock文件,可是他的other是沒有讀權限的,nginx調用的時候就沒有度權限,因此會報502的錯誤
pm.max_requests = 500 #web給php的請求提供服務的生命週期,500指的是這個進程最可能是隻能處理500個進程,當500個請求完成以後,就總銷燬
rlimit_files = 1024 :文件描述符的數量
2、配置多個pool的做用是提供給多個nginx的虛擬主機使用
3、配置慢執行日誌
做用: 慢性日誌主要是用來進行性能追蹤,定位php腳本哪裏有問題的
①在/usr/local/php/etc/php-fpm.conf文件中加入如下兩行
slowlog = /path/to/slow.log #這個位置能夠自定義
request_slowlog_timeout = 1
具體位置如圖:
②檢查配置文件
[root@mysql ~]# /usr/local/php/sbin/php-fpm -t
[21-Jun-2015 17:29:30] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
這提示表示配置文件時OK的
③定義open_basedir,加入如下一行
加入這一行:php_admin_value[open_basedir]=/data/www/:/tmp/
如圖:
④退出保存,檢查配置文件
[root@mysql ~]# /usr/local/php/sbin/php-fpm -t
[21-Jun-2015 17:44:40] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
⑤配置文件解釋:
[global]:全局配置
pid:指定進程id文件
error_log:指定錯誤文件存放的位置
[www]:資源池的名字,有多個資源池時了能夠填寫數字加以區分
listen:監聽方式,配置要和nginx中的配置一致,有兩種方式:
第一種:ip+端口的
第二種:使用sock文件
user:啓動進程的用戶,這裏的用戶要和啓動nginx的同樣
group:啓動帳戶的用戶組
pm = static/dynamic:動態、靜態子進程
若是選擇static,則由pm.max_children指定固定的子進程數。
若是選擇dynamic,則由如下參數決定:
pm.max_children :子進程最大數
pm.start_servers :啓動時的進程數
pm.min_spare_servers :保證空閒進程數最小值,若是空閒進程小於此值,則建立新的子進程
pm.max_spare_servers :保證空閒進程數最大值,若是空閒進程大於此值,此進行清理
對於專用服務器,pm能夠設置爲static。
⑥每個池子能夠單獨寫一個慢日誌,固然日誌的存放路徑是能夠本身定義的
三. nginx高級配置
1. 配置第二個虛擬主機
能夠在nginx.conf 加一行(這裏跟上面重複了哈)
include conf/vhosts/*.conf;
這樣,咱們就能夠在 conf/vhosts目錄下建立虛擬主機配置文件了。
vim conf/vhosts/111.conf // 加入
server
{
listen 80;
server_name 111.com;
index index.html index.htm index.php;
root /data/www2;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;
}
}
2. 驗證默認虛擬主機
listen 80 default_server;
3. 用戶認證
首先須要安裝apache,可使用yum install httpd 安裝
1) 安裝Apache
yum install httpd
2)查看Apache安裝的目錄
rpm -ql httpd
3、使用which htpasswd查找htpasswd文件路徑
[root@mysql ~]# which htpasswd
/usr/bin/htpasswd
4)生成密碼文件,建立用戶
添加cheng用戶,第一次添加時須要加-c參數,第二次添加時不須要-c參數
[root@mysql ~]# /usr/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd cheng
New password:
Re-type new password:
Adding password for user cheng
5)在nginx的配置文件中添加, 認證方訪問方式有兩種:
第一種:針對整個網站的一個認證
location / {
root /data/www/wwwroot/count; #在這裏添加root以後,用戶訪問網站都會要求輸入帳號和密碼,通常咱們使用第二種方式
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd; #用戶認證密碼和帳戶存放的位置
}
如圖:
保存退出
6) 檢測配置文件
[root@mysql ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
7)重啓
[root@mysql ~]# /etc/init.d/nginx reload
8)在瀏覽器測試
第二種:針對某個目錄去限制,而不是整個網站
代碼以下:
root /data/www;
location /w/ { #這裏的w就是用戶認證目錄,用戶訪問這個目錄下的文件時會要求輸入帳號和密碼來進行認證。
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
}
具體如圖:
測試:
訪問/data/www/1.php文件,以下圖所示:
訪問/data/www/w/目錄下的index2.html文件,提示要輸入帳號和密碼
4. 域名重定向
1)打開虛擬主機配置文件,咱們這裏以default2.conf主機爲例
在/usr/local/nginx/conf/vhosts/default2.conf文件中加入如下內容:
如圖:
內容:
server_name www.guhantai.com.cn www.guhantai.cn;
if ($host != 'www.guhantai.cn' ) {
rewrite ^/(.*)$ http://www.guhantai.cn/$1 permanent;
}
退出保存
2)檢查配置文件
[root@mysql ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3)重啓
[root@mysql ~]# /etc/init.d/nginx reload
從新載入 Nginx:
4)測試
[root@mysql ~]# curl -I -x127.0.0.1:80 www.guhantai.com.cn
HTTP/1.1 301 Moved Permanently #顯示的狀態是301#
Server: nginx/1.6.2
Date: Sun, 21 Jun 2015 20:28:16 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.guhantai.cn/ #跳轉成功#
5. 日誌相關
1)日誌切割:
① 編寫腳本:
vim /usr/local/sbin/logrotate.sh #/logrotate.sh這個文件默認是沒有了,使用vim新建的
加入如下內容:
#! /bin/bash
d=`date -d "-1 day" +%Y%m%d`
/bin/mv /home/logs/default2.log /home/logs/default2_$d.log #重命名,移動位置
/etc/init.d/nginx reload >/dev/null 2> /dev/null
cd /home/logs #進入/home/logs/目錄
gzip default2_$d.log #壓縮default2_$d.log文件,以便節省空間
使用zcat能夠查看壓縮的文件
保存退出
②定義日誌
打開/usr/local/nginx/conf/vhosts/default2.conf配置文件
加上如下內容:
access_log /home/logs/default2.log combined_realip;
加在以下圖所示的位置:
combined_realip:日誌的格式,是在nginx.conf文件中定義的,如圖:
③建立/home/logs/目錄
[root@mysql ~]# mkdir /home/logs/
④檢查配置文件
[root@mysql ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
⑤從新加載
[root@mysql ~]# /usr/local/nginx/sbin/nginx -s reload
⑥測試下,看看是否產生的有日誌
[root@mysql ~]# curl -I -x127.0.0.1:80 www.guhantai.com.cn #執行了兩次
⑦查看日誌,有兩行,由於上面執行了兩次,全部有兩行
[root@mysql ~]# cat /home/logs/default2.log
127.0.0.1 - [22/Jun/2015:05:36:01 +0800]www.guhantai.com.cn "/" 301"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
127.0.0.1 - [22/Jun/2015:05:36:03 +0800]www.guhantai.com.cn "/" 301"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
⑧執行腳本,在/usr/local/nginx/conf/vhosts目錄下
[root@mysql vhosts]# sh -x /usr/local/sbin/logrotate.sh
-x:是爲了查看執行過程
使用ls查看/home/logs/目錄是,會有一下兩個文件
[root@mysql vhosts]#ls /home/logs/ default2_20150621.log default2.log
-d "-1 day":指的是昨天的日誌
2)日誌格式
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format main1 '$proxy_add_x_forwarded_for - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"'; //此日誌格式爲,ip不只記錄代理的ip還記錄遠程客戶端真實IP。
3)錯誤日誌error_log日誌級別
error_log 級別分爲 debug, info, notice, warn, error, crit 默認爲crit, 該級別在日誌名後邊定義格式以下:
error_log /your/path/error.log crit;
crit 記錄的日誌最少,而debug記錄的日誌最多。若是你的nginx遇到一些問題,好比502比較頻繁出現,可是看默認的error_log並無看到有意義的信息,那麼就能夠調一下錯誤日誌的級別,當你調成error級別時,錯誤日誌記錄的內容會更加豐富。
日誌格式定義的位置在/usr/local/nginx/conf/nginx.conf配置文件中,具體如圖:
6. 配置緩存
是針對某個網站來講作的,因此配置在虛擬主機中,仍是以default2.conf虛擬主機爲例
1)編輯/usr/local/nginx/conf/vhosts/default2.conf配置文件,將如下內容拷貝到文件中去
內容以下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
location ~ :匹配的意思
(gif|jpg|jpeg|png|bmp|swf):以這些結尾的文件,設定的緩存時間爲30天
expires:緩存時間
access_log off:不記錄日誌
如圖:
保存退出
2)檢查配置文件
[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3)從新加載
[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload
4)測試
[root@mysql vhosts]# curl -I -x127.0.0.1:80 www.guhantai.cn/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 22 Jun 2015 01:17:52 GMT
Content-Type: p_w_picpath/jpeg
Content-Length: 0
Last-Modified: Mon, 22 Jun 2015 01:16:22 GMT
Connection: keep-alive
ETag: "558761e6-0"
Expires: Wed, 22 Jul 2015 01:17:52 GMT
Cache-Control: max-age=2592000 #這裏換算過來正好30天,固然能我也不會算
Accept-Ranges: bytes
7. 防盜鏈
1)在虛擬主機重配置,寫入一下內容,仍是以default2.conf虛擬主機爲例
內容以下:
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
valid_referers none blocked server_names *.taobao.com *.baidu.com *.google.com *.google.cn *.soso.com ; // 對這些域名的網站不進行盜鏈。
if ($invalid_referer) {
# return 403;
rewrite ^/ http://www.example.com/nophoto.gif;
}
}
2)若是前面配置中(指的是前面的靜態緩存配置)已經加了
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
access_log off;
}
那麼會和這一部分重複,就要把前面的註釋掉,因此結合起來的配置以下:
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 10d;
valid_referers none blocked server_names *.guhantai.com.cn *.guhantai.cn *.b.com *.baidu.com\
*.google.com *.google.cn *.soso.com ;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.example.com/nophoto.gif;
}
access_log off;
}
①退出保存
②檢查配置文件
[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
③從新加載nginx
[root@mysql vhosts]# /usr/local/nginx/sbin/nginx -s reload
④測試
不容許盜鏈測試
[root@mysql vhosts]# curl -x127.0.0.1:80 -e "http://abg.com/345" -I 'http://www.guhantai.cn/w/1.jpg'
HTTP/1.1 403 Forbidden #這裏是403
Server: nginx/1.6.2
Date: Mon, 22 Jun 2015 03:12:20 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
"http://abg.com/345: 這個網站是虛擬出來的一個,沒有在容許的範圍之類,因此報錯403的錯誤
容許盜鏈測試
[root@mysql vhosts]# curl -x127.0.0.1:80 -e "http://www.guhantai.com.cn/345" -I 'http://www.guhantai.cn/w/1.jpg'
HTTP/1.1 404 Not Found #這裏是404,由於個人網站下面根本就沒有345這個目錄,因此提示爲找到
Server: nginx/1.6.2
Date: Mon, 22 Jun 2015 03:25:01 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
⑤查看日誌
[root@mysql ~]# less /home/logs/default2.log
⑥不指定網站去訪問時,是OK的
[root@mysql vhosts]# curl -x127.0.0.1:80 -I 'http://www.guhantai.cn/w/123.jpg'
HTTP/1.1 200 OK #這裏200說明是OK的
Server: nginx/1.6.2
Date: Mon, 22 Jun 2015 03:39:04 GMT
Content-Type: p_w_picpath/jpeg
Content-Length: 568777
Last-Modified: Mon, 22 Jun 2015 03:38:17 GMT
Connection: keep-alive
ETag: "55878329-8adc9"
Expires: Thu, 02 Jul 2015 03:39:04 GMT
Cache-Control: max-age=864000
Accept-Ranges: bytes
筆記有錯誤的地方還請大神指正,小白會繼續修改