1. Default Virtual Host
Modify configuration files
vim /usr/local/nginx/conf/nginx.conf
Add "include vhost/*.conf;" in the last penultimate line (all files ending with. conf under vhost will be loaded so that all virtual host configuration files can be placed in the vhost directory)
Create vhost, edit configuration file
mkdir /usr/local/nginx/conf/vhost cd /usr/local/nginx/conf/vhost/ vim default.conf
#Add the following code server { listen 80 default_server; server_name cheese.com; index index.html index.htm index.php; root /data/nginx/default; }
Check for errors
/usr/local/nginx/sbin/nginx -t
restart
/usr/local/nginx/sbin/nginx -s reload
Create index pages
echo " test.com " > /data/nginx/test.com/index.html
Visit
curl -I -x127.0.0.1:80 test.com
2. User authentication
Create a new virtual host
cd /usr/local/nginx/conf/vhost/ vim default.conf
#Add the following server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/nginx/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
Auth_basic opens authentication, auth_basic_user_file specifies user password file
With htpasswd of httpd:
yum install -y httpd htpasswd -c /usr/local/nginx/conf/htpasswd admin
Check for errors and restart
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
Verification with curl command
mkdir /data/nginx/test.com echo " test.com " > /data/nginx/test.com/index.html curl -I -x127.0.0.1:80 test.com
401 indicates that authentication is required, and browsers can be used to authenticate.
Authenticate the directory by adding a path after location
vim /usr/local/nginx/conf/vhost/test.com.conf
server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/nginx/test.com; location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
echo "directory auth" > /data/nginx/test.com/admin/index.html
3. Domain name redirection
Editing configuration files
vim /usr/local/nginx/conf/vhost/test.com.conf
server { listen 80; server_name test.com test1.com; index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
Ser_name can be followed by multiple domain names, and permanent is a permanent redirection, equivalent to R=301 of httpd
Detection, restart, testing
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload curl -x127.0.0.1:80 test1.com/123.txt -I
Result
4. Nginx access log
Editing configuration files
vim /usr/local/nginx/conf/vhost/test.com.conf
Add access_log/tmp/test.log combined_realip;.
server { listen 80; server_name test.com test1.com; index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } access_log /tmp/test.log combined_realip; }
access_log to specify the location of the log, followed by the format name of the specified log
Verification, restart, verification
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload curl -x127.0.0.1:80 test.com/test
See
cat /tmp/test.log
Cutting Nginx logs requires the help of system cutting tools or custom scripts
vim /usr/local/sbin/nginx_log_rotate.sh
#Add the following #!/bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp" #logdir is the path where nginx logs are stored as / 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`
Increased mission plan
/bin/bash /usr/local/sbin/nginx_log_rotate.sh
5. Configure static files not to log and add expiration time
Editing configuration files
vim /usr/local/nginx/conf/vhost/test.com.conf
server { listen 80; server_name test.com test1.com; index index.html index.htm index.php; root /data/nginx/test.com; location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/test.log combined_realip; }
location~can specify the corresponding static file, expires configuration expiration time, access_log off can not record access log
Testing (creating js, jpg, jss files)
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload echo "ok is ok" > /data/nginx/test.com/01.js echo "good is good" > /data/nginx/test.com/02.jpg touch /data/nginx/test.com/01.jss
Verification
curl -I -x127.0.0.1:80 test.com/01.js curl -I -x127.0.0.1:80 test.com/02.jpg curl -I -x127.0.0.1:80 test.com/01.jss
You can see the corresponding time size of Cache-control, or you can see the access log.
cat /tmp/test.log
As you can see, there is only jss, no js and jpg