How to configure common functions after Nginx installation

王林
Release: 2023-05-15 21:19:11
forward
1152 people have browsed it

1. Separation of the main configuration file and the virtual host

If there are many virtual hosts, it will be more convenient to separate them. They can also be divided according to functions and services. The following takes two virtual hosts as an example.

Complete configuration file after removing blank lines and comments:

[root@nginx-01 conf]# egrep -v "#|^$" nginx.conf.bak 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
Copy after login

Create the virtual host configuration directory under the /app/nginx/conf directory

mkdir extra
Copy after login

Use the server module to create www and bbs two virtual sites

[root@nginx-01 conf]# cat -n nginx.conf
[root@nginx-01 conf]# sed -n '10,20p' nginx.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
Copy after login

www site

[root@nginx-01 conf]# cat extra/www.conf 
    server {
        listen       80;
        server_name  www.yygg.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
Copy after login

bbs site

[root@nginx-01 conf]# cat extra/bbs.conf 
    server {
        listen       80;
        server_name  bbs.yygg.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/bbs;
        }
    }
Copy after login

Main configuration file configuration (nginx.conf)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
include extra/www.conf;
include extra/bbs.conf;
}
Copy after login

Check configuration

[root@nginx-01 conf]# /app/nginx/sbin/nginx -t
nginx: the configuration file /app/nginx-1.18.0//conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.18.0//conf/nginx.conf test is successful
Copy after login

Create the site directory

[root@nginx-01 conf]# mkdir /app/nginx/html/{www,bbs}
[root@nginx-01 conf]# echo "http://www.yygg.com" >>/app/nginx/html/www/index.html
[root@nginx-01 conf]# echo "http://bbs.yygg.com" >>/app/nginx/html/bbs/index.html
[root@nginx-01 conf]# echo "192.168.1.5 www.yygg.com bbs.yygg.com" >>/etc/hosts
Copy after login

Start the service and test

[root@nginx-01 conf]# /app/nginx/sbin/nginx
[root@nginx-01 conf]# curl www.yygg.com
http://www.yygg.com
[root@nginx-01 conf]# curl bbs.yygg.com
http://bbs.yygg.com
Copy after login

2. Virtual host alias setting

Take the www site as an example, set the alias
The so-called alias is to set one or more additional domain names

in addition to the main domain name to www.yygg.com and set an alias yygg.com.

[root@nginx-01 conf]# cat extra/www.conf 
    server {
        listen       80;
        server_name  www.yygg.com yygg.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/www;
        }
    }
Copy after login

Restart nginx test

[root@nginx-01 conf]# /app/nginx/sbin/nginx -s reload
[root@nginx-01 conf]# cat /etc/hosts
192.168.1.5 www.yygg.com bbs.yygg.com yygg.com
[root@nginx-01 conf]# curl yygg.com
http://www.yygg.com
Copy after login

3.Nginx status status information configuration

Status information recording is implemented using the `ngx_http_stub_status_module` module

Input/app/nginx/sbin/nginx -VCheck whether the above module is included in the compilation:

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
Copy after login

Create a status virtual host, refer to title 1 for the method, status.confConfiguration The file is as follows:

    server {
        listen       80;
        server_name  status.yygg.com;
        location / {
            stub_status on;
            access_log off;
        }
    }
Copy after login

Main configuration filenginx.confAppend status virtual host configuration

sed -i '11 i include extra/status.conf;' nginx.conf
Copy after login

Check the syntax and restart nginx

/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx -s reload
Copy after login

Configure hosts resolution

192.168.1.5 status.yygg.com

Visit status.yygg.comView

[root@nginx-01 conf]# curl status.yygg.com
Active connections: 1 
server accepts handled requests
 4 4 4 
Reading: 0 Writing: 1 Waiting: 0
Copy after login

Display result analysis:

Active connections: 1 ##The number of connections being processed is 1
server ##A total of 4 connections have been processed
accepts ##A total of 4 handshakes have been created
handled requests ##A total of 4 processes have been processed Requests
Reading ##The number of Header information read to the client
Writing ##The number of Header information returned to the client
Waiting ##NGinx has finished processing the resident connection that is waiting for the next request command Number

4. Add error log

error_log syntax:

error_log file level;

keyword remains unchanged , file is the log storage location, level is the error log level

Usually only three levels of warn|error|crit are used

Configure the error log configuration in nging.conf In the file, add

error_loglogs/error_log;

under
worker_processes 1;

Yes, this is the line!

The above is the detailed content of How to configure common functions after Nginx installation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template