vim /usr/local/nginx/conf/nginx.confjavascript
*下面的要刪除掉* 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; } }
gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; *須要增長* include vhost/*.conf; }
[root@aminglinux-01 conf]# cd vhost/ [root@aminglinux-01 vhost]# vim aaa.com.conf
server { listen 80 default_server; // 有這個標記的就是默認虛擬主機 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
[root@aminglinux-01 vhost]# mkdir -p /data/wwwroot/default [root@aminglinux-01 vhost]# cd /data/wwwroot/default/
vim index.html 寫入 This is the default site.php
[root@aminglinux-01 default]# /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@aminglinux-01 default]#
[root@aminglinux-01 conf]# curl localhost This is the default site.
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; } }
[root@aminglinux-01 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd aming New password: Re-type new password: Adding password for user aming [root@aminglinux-01 vhost]#
[root@aminglinux-01 vhost]# curl -x192.168.245.130:80 test.com <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.8.0</center> </body> </html> [root@aminglinux-01 vhost]#
401 說明拒絕訪問,再用用戶名密碼試一次 curl -uaming:123456 -x192.168.245.130:80 test.comcss
[root@aminglinux-01 vhost]# curl -uaming:123456 -x192.168.245.130:80 test.com <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.8.0</center> </body> </html> [root@aminglinux-01 vhost]#
404 是由於尚未建立test.com 的主目錄html
[root@aminglinux-01 vhost]# mkdir /data/wwwroot/test.com [root@aminglinux-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html [root@aminglinux-01 vhost]# curl -uaming:123456 -x192.168.245.130:80 test.com test.com [root@aminglinux-01 vhost]#
location /admin/ 這個後面直接加上想限制的文件或者目錄就能夠了。 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
server_name後面支持寫多個域名,這裏要和httpd的作一個對比java
permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302mysql
^/(.*)$ :前面的^表明着域名,linux
/(.*)$後面表明着域名後面的內容nginx
vim /usr/local/nginx/conf/nginx.conf //搜索log_formatsql
$remote_addr 客戶端IP(公網IP) $http_x_forwarded_for 代理服務器的IP $time_local 服務器本地時間 $host 訪問主機名(域名) $request_uri 訪問的url地址 $status 狀態碼 $http_referer referer $http_user_agent user_agent
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; } access_log /tmp/1.log combined_realip; }
這裏的combined_realip就是在nginx.conf中定義的日誌格式名字shell
檢查,從新加載 -t && -s reload
測試:curl -x192.168.245.130:80 test.com -I
cat /tmp/1.log
[root@aminglinux-01 vhost]# curl -x192.168.245.130:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.8.0 Date: Sat, 21 Oct 2017 01:25:36 GMT Content-Type: text/html Content-Length: 194 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@aminglinux-01 vhost]# cat /tmp/1.log 192.168.245.130 - [21/Oct/2017:09:25:36 +0800] test.com "/" 401 "-" "curl/7.29.0" [root@aminglinux-01 vhost]#
由於nginx沒有自帶的切割工具,因此須要寫一個shell腳本
#! /bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp/" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid` ~
[root@aminglinux-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d '-1 day' +%Y%m%d + d=20171020 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls 1.log + for log in '`ls *.log`' + mv 1.log 1.log-20171020 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 850 + /root /usr/local/sbin/nginx_logrotate.sh:行11: /root: 是一個目錄
[root@aminglinux-01 vhost]# ls /tmp/ 1.log 1.log-20171020 mysql.sock pear php-fcgi.sock systemd-private-b9931a4a12de47bfa443a28713c6f410-vmtoolsd.service-Fu8IIH [root@aminglinux-01 vhost]#
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
先註釋掉以前的配置
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ # { # expires 7d; # access_log off; # }
增長防盜鏈配置
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { expires 7d; valid_referers none blocked server_names *.test.com ; if ($invalid_referer) { return 403; } access_log off; }
增長配置
location /admin/ { allow 127.0.0.1; allow 192.168.245.130; deny all; }
只有allow,才能經過訪問。其餘都會被拒絕。
[root@aminglinux-01 ~]# vi /usr/local/nginx/conf/vhost/test.com.conf [root@aminglinux-01 ~]# mkdir /data/wwwroot/test.com/admin/ [root@aminglinux-01 ~]# echo 「test,test」>/data/wwwroot/test.com/admin/1.html [root@aminglinux-01 ~]# /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@aminglinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload [root@aminglinux-01 ~]# curl -x192.168.245.130:80 test.com/admin/1.html -I HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Tue, 24 Oct 2017 04:21:33 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Tue, 24 Oct 2017 04:19:08 GMT Connection: keep-alive ETag: "59eebf3c-10" Accept-Ranges: bytes
加上這一條配置
location ~ .*(abc|image)/.*\.php$ { deny all; }
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
vi /usr/local/nginx/conf/vhost/test.com.conf 加入
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }
配置以下內容,就能夠經過本機來訪問ask.apelearn.com
server { listen 80; server_name ask.apelearn.com; location / { proxy_pass http://121.201.9.155/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }