LNMP architecture (Nginx installation, Nginx default virtual host, Nginx user authentication, Nginx domain name redirection)

Keywords: Nginx PHP curl git

Nginx installation

1. Enter the / usr/local/src / directory to download the installation package

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.3.tar.gz

2. Unzip installation package

tar zxf nginx-1.6.3.tar.gz

3. Enter the source directory for compilation, and do not add too many plug-ins here for the time being. Later, ssl may be added to support https.

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

4. installation

make && make install

5. View nginx configuration file

[root@yolks2 nginx-1.6.3]# ls /usr/local/nginx/conf/  #View the configuration file directory
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@yolks2 nginx-1.6.3]# ls /usr/local/nginx/html/  #View the html sample file directory
50x.html  index.html

6. Commands to check that the configuration file is correct

[root@yolks2 nginx-1.6.3]# /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

7. Add startup script: / etc/init.d/nginx, file content reference: nginx startup script reference profile content The specific code is as follows:

#!/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

8. Change permissions: 755

chmod 755 /etc/init.d/nginx

9. Add boot start item and open it

chkconfig --add nginx
chkconfig nginx on

10. Back up the original configuration file and add parameters to the newly edited file

[root@yolks2 conf]# mv nginx.conf nginx.conf_bak
[root@yolks2 conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf_bak      scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default

New document reference template: nginx configuration file template reference The code is as follows:

user nobody nobody; #In which capacity does nginx access resources
worker_processes 2; #There are several subprocesses defined
error_log /usr/local/nginx/logs/nginx_error.log crit; #Error log
pid /usr/local/nginx/logs/nginx.pid; #pid
worker_rlimit_nofile 51200; #How many files can nginx open at most

events
{
    use epoll; #epoll mode
    worker_connections 6000; #How many connections can a process have at most
}

http #http correlation
{
    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 #Default Virtual Host
    {
        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_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    }
}

Check the configuration file for errors

[root@yolks2 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

11. Start nginx

[root@yolks2 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  Sure?  ]

View process

[root@yolks2 conf]# ps aux |grep nginx
root       3732  0.0  0.0  24852   776 ?        Ss   21:56   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     3733  0.0  0.1  27296  3356 ?        S    21:56   0:00 nginx: worker process
nobody     3734  0.0  0.1  27296  3356 ?        S    21:56   0:00 nginx: worker process
root       3736  0.0  0.0 112720   984 pts/0    R+   21:56   0:00 grep --color=auto nginx

View subprocesses

ps aux |grep php-fpm

View port number

netstat -lntp

Use curl to test for patency

curl localhost/curl 127.0.0.1

Nginx welcome page: path from **/usr/local/nginx/html/index.html**

12. Test nginx to parse php files: / usr/local/nginx/html/1.php

<?php
echo "this is the test nginx page!";
?>

nginx default virtual host

1. Comment out the server configuration in the configuration file, or delete it directly

2. Add the following code

include vhost/*.conf;

3. Create the directory / usr/local/nginx/conf/vhost/aaa.com.conf and add the following configuration

[root@yolks2 conf]# pwd
/usr/local/nginx/conf
[root@yolks2 conf]# mkdir vhost
[root@yolks2 conf]# cd vhost/
[root@yolks2 vhost]# vim aaa.com.conf

Add the configuration code as follows

server
{
    listen 80 default_server;  // The default virtual host has this tag
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

4. Create directories

mkdir -p /data/wwwroot/default/

5. Edit and test html files

This is a default site.

6. Check if the configuration file is incorrect, and then reload the configuration file

/usr/local/nginx/sbin/nginx -t #Check that the configuration file is correct
/usr/local/nginx/sbin/nginx -s reload #Reload

7.curl test default virtual host parsing page

[root@yolks2 default]# curl -x127.0.0.1:80 aaa.com
This is a default site.

Nginx User Authentication

1. Create a new test.com.conf configuration file in the / usr/local/nginx/conf/vhost/directory as follows:

server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
    location  /
    {
        auth_basic              "Auth"; #User Authentication Name
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd; #User name password file
    }
}

2. Generate password files

Install password generation tools

yum install -y httpd

Generate password

Htpasswd-c saves location username
 Example: htpasswd-c/usr/local/nginx/conf/htpasswd yolks

Practice: Here the password is set to 12345678

[root@yolks2 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd yolks
New password: 
Re-type new password: 
Adding password for user yolks
[root@yolks2 vhost]# cat /usr/local/nginx/conf/htpasswd
yolks:$apr1$GEm0a4zp$oL89HjmRG9YIcUG0eyLsQ/

3. Check the files and reload them

/usr/local/nginx/sbin/nginx -t #Check that the configuration file is correct
/usr/local/nginx/sbin/nginx -s reload #Reload

4. test

[root@yolks2 vhost]# curl -x127.0.0.1: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.6.3</center>
</body>
</html>

5. Create the / data / wwroot / test directory and create the test file index.html

[root@yolks2 vhost]# mkdir -p /data/wwwroot/test.com
[root@yolks2 vhost]# cd !$
cd /data/wwwroot/test.com
[root@yolks2 test.com]# ls
[root@yolks2 test.com]# echo "test.com" >> /data/wwwroot/test.com/index.html

6. Testing again: 200:ok

[root@yolks2 test.com]# curl -uyolks:12345678 -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 12 Aug 2018 15:00:04 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Sun, 12 Aug 2018 14:58:57 GMT
Connection: keep-alive
ETag: "5b704b31-9"
Accept-Ranges: bytes

7. Modify the configuration file if you restrict the directory

8. Create corresponding admin directory and test html page

[root@yolks2 test.com]# cd /data/wwwroot/test.com/
[root@yolks2 test.com]# ls
index.html
[root@yolks2 test.com]# mkdir admin #Create admin directory
[root@yolks2 test.com]# ls
admin  index.html
[root@yolks2 test.com]# echo "this is admin page." >> admin/admin.html #Create test files
[root@yolks2 test.com]# curl -uyolks:12345678 -x127.0.0.1:80 test.com/admin/admin.html -I #View the test page
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 12 Aug 2018 15:04:51 GMT
Content-Type: text/html
Content-Length: 20
Last-Modified: Sun, 12 Aug 2018 15:04:31 GMT
Connection: keep-alive
ETag: "5b704c7f-14"
Accept-Ranges: bytes

[root@yolks2 test.com]# curl -uyolks:12345678 -x127.0.0.1:80 test.com/admin/admin.html #View test page results
this is admin page.

9. Restricting file access: matching files

Nginx domain name redirection

1. Change the file / usr/local/nginx/conf/vhost/test.com.conf, code as follows:

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' ) { #If the domain name is not equal to test.com, jump
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
}

2. Check the configuration file and reload it

/usr/local/nginx/sbin/nginx -t #Check that the configuration file is correct
/usr/local/nginx/sbin/nginx -s reload #Reload

3. test

[root@yolks2 test.com]# curl -x127.0.0.1:80 test2.com/index.html -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Sun, 12 Aug 2018 15:18:38 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html # indicates that the link is here
[root@yolks2 test.com]# curl -x127.0.0.1:80 test2.com/admin/admin.html -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Sun, 12 Aug 2018 15:20:06 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://The test.com/admin/admin.html directory also follows the jump

Expand

extend Detailed configuration of nginx.conf http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880
nginx rewrite four flag s http://www.netingcn.com/nginx-rewrite-flag.html
http://unixman.blog.51cto.com/10163040/1711943

Posted by zildjohn01 on Tue, 08 Jan 2019 15:36:10 -0800