Nginx installation, default virtual host, Nginx user authentication, Nginx domain name redirection

Nginx installation:

 

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.1.tar.gz = nginx download address (or you can go to the official website to download directly)

tar zxf nginx-1.12.1.tar.gz = unzip the download package

cd nginx-1.12.1 = enter the decompressed directory

./configure --prefix=/usr/local/nginx = compile nginx (you can add required modules according to your needs when compiling)

make && make install = continue installation

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?public=true ) = edit startup script

#nginx启动脚本配置文件
#!/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

chmod 755 /etc/init.d/nginx = give start script permissions

chkconfig --add nginx = add the boot service to start automatically

chkconfig nginx on = enable the boot service to start automatically on=on off=off

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak = enter the nginx configuration directory and change a name to the original configuration file

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

#nginx配置文件
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;
        }    
    }
}

/usr/local/nginx/sbin/nginx -t = nginx to see if there are any errors in its own syntax

/etc/init.d/nginx start = start nginx

netstat -lntp |grep 80 = view port nginx listens on port 80

systemctl status nginx.service = If there is a problem with starting nginx, you can use the command to determine where the problem is

 

vi /usr/local/nginx/html/1.php //Add the following content
 <?php
    echo "test php scripts.";
?>
 curl localhost/1.php

 

Nginx default virtual host:

vim /usr/local/nginx/conf/nginx.conf //Add = change the original configuration file

include vhost/*.conf = add newly defined configuration files

mkdir /usr/local/nginx/conf/vhost = create a new vhost directory under the conf directory

cd !$; vim default.conf //Add the following content = Write the new configuration in the default.conf file in the vhost directory !$=The last command executed

#配置文件
server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

mkdir -p /data/wwwroot/default/ = the root directory of the website defined in the configuration (if there is one, you can not create it)

cd /data/wwwroot/default/ = Go to the root directory of the created website and write something to test it

vim index.html = defines a test configuration file echo "This is a default site."

echo “This is a default site.”>/data/wwwroot/default/index.html = can be written directly

/usr/local/nginx/sbin/nginx -t = Judgment syntax error

/usr/local/nginx/sbin/nginx -s reload = reload configuration file

curl localhost = test parsing local machine

curl -x127.0.0.1:80 123.com = test resolving other domain names

 

Nginx user authentication:

vim /usr/local/nginx/conf/vhost/test.com.conf// write the following = create a new virtual host

#虚拟主机配置文件
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;          =定义用户密码文件
}
}

yum install -y httpd = tool requirements install

htpasswd -c /usr/local/nginx/conf/htpasswd aming = generate a user name and password file (if you need to continue to generate a second user and password then do not add -c  )

   cat can view the generated user and password with

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

[root@aming-01 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@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload     =  重新加载配置文件

mkdir /data/wwwroot/test.com = create a test configuration file directory

echo "test.com" >/data/wwwroot/test.com/index.html = Test configuration file created

curl -x127.0.0.1:80 test.com -I//If the status code is 401, it means authentication is required = test If 401 appears, it means the user needs to be authenticated

curl -uaming:passwd access status code becomes 200 = test status after authentication user is 200

[root@aming-01 vhost]# curl -uaming:rabbit -x127.0.0.1:80 test.com
“test.com”

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 for the user authentication of the directory

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

 

Nginx domain name redirection:

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 explanation of nginx.conf configuration  http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880
nginx rewrite four flags 

http://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=325194576&siteId=291194637