12.6-12.9 Nginx installation, default virtual host, domain name redirection

12.6 Nginx installation

outline

blob.png

blob.png

1 Enter the src directory and download nginx in this directory

 #cd  /usr/local/src

 #wget http://nginx.org/download/nginx-1.8.0.tar.gz

2 Unzip the compressed package

 #tar zxf nginx-1.12.1.tar.gz

3 Compile and install

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

 #make &&  make install

The core program of nginx can also use -t to check the status.

[root@AliKvn usr]# ls /usr/local/nginx/sbin/nginx 

/usr/local/nginx/sbin/nginx

[root@AliKvn usr]# /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

4 Edit the nginx configuration file

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

blob.png

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

5 Change file 755 permissions

 #chmod 755 /etc/init.d/nginx

6 Add boot service

 #chkconfig --add nginx 

 #chkconfig nginx on

7 Configure the configuration file of Nginx

 #cd /usr/local/nginx/conf/ 

 #mv nginx.conf nginx.conf.1

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

blob.png

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;
    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;
        }    
    }
}

Configuration file parameter parsing:

userDefine which user to start Nginx with

worker_processes 2  The startup process has 2 child processes

worker_rlimit_nofile 51200The maximum number of files that Nginx can open is 51200

use epoll;Use epoll mode

worker_connections 6000Process has up to 6000 links

The server part corresponds to the v-host virtual host of httpd

server_name domain name

location php parsing php related parameters part

root web page path

blob.png

!!!! Generally, there is an error or failure in listening to port 80. Most of them are closely related to the configuration parameters of the server. It is also possible that httpd has also been started, causing port 80 to be occupied.

8 After editing, check the status, process and port

[root@AliKvn conf]# /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

Check for errors, try to start the service

(Note that if httpd is started, port 80 will be occupied, so it must be closed, otherwise nginx will fail to start).

The failure error is generally as follows:

blob.png

In this case, you need to close httpd, and then continue to start nginx.

[root@AliKvn conf]# /etc/init.d/nginx start

Starting nginx (via systemctl):                            [  OK  ]

 #ps aux |grep nginx check process, there are 2 child processes (two nobody)

Ss represents the parent process. Generally, the parent process user is root, and the child process is nobody.

blob.png

check port,

#netstat -lntp | grep 80

blob.png

9 Test php parsing

create php file

#vim /usr/local/nginx/html/1.php

<?php
echo "this is the Nginx test page.";

curl check php parsing test

[root@AliKvn conf]# curl 127.0.0.1/1.php

this is the Nginx test page.[root@AliKvn conf]# 



12.7 Nginx default virtual host

outline

blob.png

There is also a default virtual host in Nginx. Similar to httpd, the first virtual host loaded by Nginx is the default host.

Unlike httpd, in Nginx, default_server can be used to mark the default virtual host. If the virtual host is not marked as default, the first virtual host will be selected as the default virtual host.

Related configuration operations

1 Modify the configuration file of nginx and add the parameter include vhost/*.conf in it 

[root@AliKvn conf]#vim /usr/local/nginx/conf/nginx.conf 

blob.png 

blob.png

Parameter explanation:

include vhost/*.conf 

Indicates that vhost/all *.conf files are supported, and include supports wildcard use


2 Create a vhost directory

[root@AliKvn conf]#  mkdir vhost

[root@AliKvn 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;
}

Parse

server

{

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

    server_name aaa.com;//site name

    index index.html index.htm index.php;//relevant index page

    root /data/wwwroot/default;//site directory

}

3 Create the default directory, enter it, define a new html, and create an index page index.html

[root@AliKvn vhost]# mkdir -p /data/wwwroot/default/

[root@AliKvn vhost]# echo “This is a default site.”>/data/wwwroot/default/index.html

-t check status

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t

-s reload, which is equivalent to the graceful usage of apache

[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

4 test

Test the default site

[root@AliKvn vhost]# curl localhost

this is a default site. 

Test the default host

[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa.com

this is a default site. 

[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa1111.com

this is a default site. 


12.8 Nginx User Authentication

outline

blob.png

blob.png

[root@AliKvn vhost]# cd /usr/local/nginx/conf/vhost

[root@AliKvn vhost]#vim /usr/local/nginx/conf/vhost/test.com.conf

1 Write the following content to establish authentication parameters (in fact, the authentication parameters are the same, but individual parameters are different, for example: server name root. Others are the same)

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;
}
}

Parse

auth_basic is the name used to define user authentication

auth_basic_user_file is used to define the file and password for user authentication. Here, the password file is generally connected.


2 Use the htpasswd tool to generate a password (if there is no htpasswd password tool, you can install httpd yum install -y httpd)

usage, make file specified user

[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming

The second user establishes **** (note that -c is the generation establishment. If it is established once, do not use -c to create it again, otherwise it will be cleared and the password will be reset)

[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd user1

3 Check and reload, -t && -s reload 

[root@AliKvn vhost]# /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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

4 curl test, the status code is 401 not authenticated.

blob.png

curl specified user test

blob.png

If you want to access the site in the directory below test.com/, you can do this configuration

Visit /test.com/admin/authentication

1 Modify the path after location and define it as

location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

2 Check and reload, -t && -s reload 

3 curl test

#curl -uaming:passwd -x127.0.0.1 test.com/admin/

Visit /test.com/admin.php for authentication and prompt for authentication. Status code 401

curl -uaming:passwd -x127.0.0.1 test.com/admin.php


12.9 Nginx domain name redirection

Nginx's domain name redirection is similar to httpd, but easier to understand. The operation is as follows:

1 Modify the configuration file test.com.conf

server
{
    listen 80;
    server_name test.com test1.com test2.com test3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
}

Parameter parsing:

 rewrite  ^/(.*)$  http://test.com/$1  permanent;

^/(.*)$ is actually equivalent to http://$host/(.*)$, then http://$host/ is equivalent to http://test.com/

/(.*)$/ indicates the subpage below the site, (.*)$ indicates the string after the wildcard, until the end, $ indicates the end.

$1 = (.*)$

$host/ can be omitted and directly becomes ^/, which means starting with xxx,

permanent means 301

!!!!

In Nginx, multiple domain names are supported after server_name. But in httpd, httpd can only carry one server name, and multiple domain names can only be defined by Alias ​​Name.

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

2 Check configuration syntax and reload -t&&-s reload

[root@AliKvn vhost]# /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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

3 curl test

First visit the following main domain name test.com status code 200, indicating that by visiting

blob.png

Visit test1.com, not test.com. Access code 301, this is because the ^ parameter comes into play.

blob.png

Visit test2.com/111/111, the status code is 301, and the ^/(.*)$ condition is satisfied.

blob.png

Guess you like

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