1、nginx配置用戶認證javascript
首先須要安裝apache,可使用yum install httpd 安裝;或者在其餘機器建立好.htpasswd文件,拷貝到服務器;php
建立用戶,並生成密碼文件:css
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd test html
// 添加test用戶,第一次添加時須要加-c參數,第二次添加時不須要-c參數;java
訪問指定目錄配置用戶認證:linux
在nginx的default配置文件中添加,紅色的部分是指定在哪一個目錄設置用戶認證。nginx
location /a/ {apache
auth_basic "Auth";緩存
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;bash
}
實驗測試,使用curl 解析/a/目錄下的index.html爲401未認證;使用-u 用戶名密碼登陸以後爲200 OK;
[root@localhost vhosts]# curl -x127.0.0.1:80 192.168.20.30/a/index.html -I HTTP/1.1 401 Unauthorized Server: nginx/1.6.2 Date: Thu, 14 May 2015 09:48:18 GMT Content-Type: text/html Content-Length: 194 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@localhost vhosts]# curl -utest:1234 -x127.0.0.1:80 192.168.20.30/a/index.html -I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Thu, 14 May 2015 09:48:26 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Thu, 14 May 2015 09:27:34 GMT Connection: keep-alive ETag: "55546a86-264" Accept-Ranges: bytes
訪問admin.php後臺,設置用戶認證;設置認證的代碼要寫在匹配php的前面,而且也要加入解析php的代碼;
[root@localhost vhosts]# cat default.conf server { listen 80 default; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ admin\.php { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/.htpasswd; 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; } 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; } }
網頁測試登陸admin.php頁面彈出認證對話框,輸入用戶名密碼才能夠訪問。
如不指定root目錄,直接使用認證的話,打開首頁彈出對話框進行認證;
location / {
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
2、配置域名重定向
nginx默認虛擬主機配置加入下面的代碼:
server_name 11.com 22.com www.111.com;
if ($host != 'www.111.com' ) {
rewrite ^/(.*)$ http://www.111.com/$1 permanent;
}
permanent 永久重定向301;
試驗測試:訪問11.com 22.com 都會跳轉到location:www.111.com;
[root@localhost vhosts]# curl -x127.0.0.1:80 11.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Thu, 14 May 2015 22:15:16 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.111.com/ [root@localhost vhosts]# curl -x127.0.0.1:80 22.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Thu, 14 May 2015 22:15:21 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.111.com/
3、配置日誌記錄
日誌格式,main爲定義的日誌格式名;日誌格式需加入到nginx.conf主配置文件http段中;
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。
添加訪問日誌的格式,寫到default.conf最後一行;
server { listen 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; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } access_log /home/logs/xxx.log combined_realip; }
combined_realip爲nginx.conf 定義的日誌格式的名字。
使用curl測試以後,產生新的log;
[root@localhost vhosts]# curl -x127.0.0.1:80 192.168.20.30/index.html -I [root@localhost vhosts]# cat /home/logs/xxx.log 127.0.0.1 - [15/May/2015:15:13:49 +0800]192.168.20.30 "/index.html" 200"-" "curl/7.19.7 (i386-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"
錯誤日誌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級別時,錯誤日誌記錄的內容會更加豐富。
4、靜態文件(圖片、flash、js、css)不記錄日誌,並配置緩存;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 12h;
access_log off;
}
expires 定義緩存時間;
access_log off 不記錄日誌;
試驗測試:touch 1.jpg 2.js文件,使用curl測試cache-control爲緩存時間;
[root@localhost vhosts]# touch /usr/local/nginx/html/1.jpg
[root@localhost vhosts]# curl -x127.0.0.1:80 www.111.com/1.jpg -I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Thu, 14 May 2015 22:11:45 GMT Content-Type: p_w_picpath/jpeg Content-Length: 0 Last-Modified: Thu, 14 May 2015 22:11:41 GMT Connection: keep-alive ETag: "55551d9d-0" Expires: Sat, 13 Jun 2015 22:11:45 GMT Cache-Control: max-age=2592000 Accept-Ranges: bytes
[root@localhost vhosts]# touch /usr/local/nginx/html/2.js [root@localhost vhosts]# curl -x127.0.0.1:80 www.111.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Thu, 14 May 2015 22:11:00 GMT Content-Type: application/javascript Content-Length: 0 Last-Modified: Thu, 14 May 2015 22:10:53 GMT Connection: keep-alive ETag: "55551d6d-0" Expires: Fri, 15 May 2015 10:11:00 GMT Cache-Control: max-age=43200 Accept-Ranges: bytes
5、防盜鏈
在nginx默認主機default.conf中的server部分中添加以下代碼:
// 對taobao、baidu、google、soso這些域名的網站不進行盜鏈。若是不是規定的域名,返回403錯誤;或者跳轉到一個自定義的圖片上;
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;
}
}
~* 表明不區分大小寫的匹配;
如同一個配置文件中都有~ 匹配的話,只匹配最上面的;把圖片文件的緩存和不記錄日誌代碼,放到一個~匹配中都生效;
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 10d;
valid_referers none blocked server_names *.1.com *.a.com *.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;
}
使用curl -e測試,須要加http:// 使用qq.com訪問圖片顯示403錯誤,使用1.com是200 OK;驗證防盜鏈成功;
[root@localhost vhosts]# curl -x127.0.0.1:80 -e "http://www.qq.com" 192.168.20.30/1.jpg -I HTTP/1.1 403 Forbidden Server: nginx/1.6.2 Date: Fri, 15 May 2015 08:28:07 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive [ root@localhost vhosts]# curl -x127.0.0.1:80 -e "http://www.1.com" 192.168.20.30/1.jpg -I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Fri, 15 May 2015 08:28:16 GMT Content-Type: p_w_picpath/jpeg Content-Length: 0 Last-Modified: Fri, 15 May 2015 07:55:55 GMT Connection: keep-alive ETag: "5555a68b-0" Accept-Ranges: bytes