12.6 Nginx installation 12.7 Default virtual host 12.8 Nginx user authentication 12.9 Nginx domain name redirection

Nginx installation

 

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.1.tar.gz

tar zvxf nginx-1.12.1.tar.gz

cd nginx-1.12.1

./configure --prefix=/usr/local/nginx

make &&  make install

ls /usr/local/nginx //View directory

conf html logs sbin

/usr/local/nginx/sbin/nginx -t (support -t, check the configuration file for errors)

//write startup script

vim /etc/init.d/nginx //Copy the following (refer to https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )

 

chmod 755 /etc/init.d/nginx //Change permissions

chkconfig --add nginx      

chkconfig nginx on //boot start

cd /usr/local/nginx/conf/

//The original nginx.conf is not used, customize one

mv nginx.conf nginx.conf.bak    

Change the nginx configuration file

//First clear the original configuration file > /usr/local/nginx/conf/nginx.conf > When used alone, a text document can be quickly cleared

vim nginx.conf //Write the following content (refer to https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf )

/usr/local/nginx/sbin/nginx -t //After saving the configuration file, check if there are any errors

/etc/init.d/nginx start //Start nginx

//If it cannot be started, check the /usr/local/nginx/logs/error.log file to check whether it has been started. The command is as follows: ps aux |grep nginx

 

netstat -lntp | grep 80

 

 

test php parsing

 

// first create the test file

vim /usr/local/nginx/html/1.php //Add the following content

<?php

    echo "test php scripts.";

?>

 

[root@localhost conf]# curl localhost/1.php

test php scripts.

 

 Nginx default virtual host

 

 

vim /usr/local/nginx/conf/nginx.conf //Modify the main configuration file, delete the server part for now, and add a line of configuration above the closing symbol }, as follows:

include vhost/*.conf;

 

mkdir /usr/local/nginx/conf/vhost

cd /usr/local/nginx/conf/vhost 

vim aaa.com.conf //Add the following content

server

{

    listen 80 default_server; // This flag is the default virtual host

    server_name aaa.com;

    index index.html index.htm index.php;

    root /data/wwwroot/default;

}

mkdir -p /data/wwwroot/default/

cd /data/wwwroot/default/

echo "This is a default site.">/data/wwwroot/default/index.html //Create a search page

/usr/local/nginx/sbin/nginx -t //Check for errors

/usr/local/nginx/sbin/nginx -s reload //reload or /etc/init.d/nginx restart restart  

curl localhost //Access aaa.com      (curl -x127.0.0.1:80  aaa.com  )

curl -x127.0.0.1:80 123.com   //Visit an undefined domain name and also access

 

 

Nginx user authentication

 

cd /usr/local/nginx/conf/vhost

vim /usr/local/nginx/conf/vhost/ test.com .conf //Create a virtual host and write the following

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;

}

}

 

(where auth_basic "Auth"; open authentication, define the user authentication name

auth_basic_user_file /usr/local/nginx/conf/htpasswd; specifies the user password file)

 

(If apache has been installed in this machine, you can use the command /usr/local/apache2.4/bin/htpasswd directly. If not, yum install -y httpd)

 /usr/local/apache2.4/bin/htpasswd  -c /usr/local/nginx/conf/htpasswd nan

cat /usr/local/nginx/conf/htpasswd

(When creating the second one, you don't need to add -c. If you add it, it will be reset.)

-t && -s reload //Test configuration and reload

(The benefit of reload, if there is an error, will not affect the configuration file, but restart does not)

 

mkdir /data/wwwroot/test.com

echo “test.com”>/data/wwwroot/test.com/index.html

curl -x127.0.0.1:80 test.com -I //The status code is 401, indicating that verification is required

curl -uaming:passwd -x127.0.0.1:80 test.com  -I     //Access status code becomes 200

curl -unan:  123123 -x127.0.0.1:80 test.com  -I

Edit the hosts file of Windows, and then visit test.com in the browser, there will be a pop-up window for entering the user and password

User Authentication for Directory

came test.com.confh

location  /admin/

    {

        auth_basic              "Auth";

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;

}

 

 

Nginx domain name redirection

 

cd /usr/local/nginx/conf/vhost

change test.com.conf _

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 supports writing multiple domain names, here is a comparison with httpd

permanent is a permanent redirect, the status code is 301, and if redirect is written, it is 302

expand

Detailed nginx.conf configuration http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880 

nginx rewrite四种flaghttp://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305995&siteId=291194637