Heim  >  Artikel  >  Betrieb und Instandhaltung  >  So konfigurieren Sie allgemeine Funktionen nach der Nginx-Installation

So konfigurieren Sie allgemeine Funktionen nach der Nginx-Installation

王林
王林nach vorne
2023-05-15 21:19:111102Durchsuche

1. Trennen Sie die Hauptkonfigurationsdatei vom virtuellen Host.

Wenn es viele virtuelle Hosts gibt, ist es bequemer, sie nach Funktionen und Diensten zu trennen.

Vervollständigen Sie die Konfigurationsdatei nach dem Entfernen von Leerzeilen und Kommentaren:

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

Erstellen Sie das Konfigurationsverzeichnis für den virtuellen Host im Verzeichnis /app/nginx/conf

mkdir extra

Verwenden Sie das Servermodul, um zwei virtuelle Sites zu erstellen: www und bbs

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

www

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

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

Konfiguration der Hauptkonfigurationsdatei (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;
}

Überprüfen Sie die Konfiguration

[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

Erstellen Sie das Site-Verzeichnis

[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

Starten Sie den Dienst und testen Sie

[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

2. Einstellung des virtuellen Host-Alias

Nehmen Legen Sie beispielsweise für die Website www einen Alias ​​fest.
Der sogenannte Alias ​​dient dazu, zusätzlich zum Hauptdomänennamen einen oder mehrere Domänennamen festzulegen.

Legen Sie den Alias ​​yygg.com für fest www.yygg.com. www.yygg.com设置别名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;
        }
    }

重启nginx测试

[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

3.Nginx status状态信息配置

状态信息记录使用的是`ngx_http_stub_status_module`模块实现

输入/app/nginx/sbin/nginx -V检查编译是否有上述模块:

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

创建一个status的虚拟主机,方式参考标题1,status.conf配置文件如下:

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

主配置文件nginx.conf追加status虚拟主机配置

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

检查语法并重启nginx

/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx -s reload

配置hosts解析

192.168.1.5 status.yygg.com

访问status.yygg.com查看

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

显示结果解析:

Active connections: 1  ##正处理的连接数为1
server  ##共处理了4次连接
accepts  ##共创建了4次握手
handled requests  ##共处理了4次请求
Reading  ##读取到客户端的Header信息数
Writing  ##返回给客户端的Header信息数
Waiting  ##NGinx已经处理完正在等候下一次请求的指令的驻留连数

4.增加错误日志

error_log语法:

error_log    file    level;

关键字不变,file是日志存放位置,level是错误日志级别

通常只用warn|error|crit三个级别

配置错误日志配置,在nging.conf文件中worker_processes 1;rrreee

Nginx-Test neu starten
rrreee

3.Nginx-Status-Statusinformationskonfiguration

Die Aufzeichnung von Statusinformationen wird mithilfe des Moduls „ngx_http_stub_status_module“ implementiert.

Geben Sie /app/nginx/sbin/nginx -V ein Überprüfen Sie beim Kompilieren, ob die oben genannten Module vorhanden sind: 🎜rrreee🎜Erstellen Sie einen virtuellen Statushost. Die Methode finden Sie in Titel 1. Die Konfigurationsdatei status.conf lautet wie folgt: 🎜rrreee🎜Die Hauptkonfiguration Datei nginx.confStatus virtuelle Hostkonfiguration anhängen🎜rrreee🎜Überprüfen Sie die Syntax und starten Sie nginx neu🎜rrreee🎜Hostauflösung konfigurieren🎜🎜192.168.1.5 status.yygg.com🎜🎜Besuchen Sie status.yygg .com zum Anzeigen von 🎜rrreee🎜Anzeige Ergebnisanalyse: 🎜🎜🎜Aktive Verbindungen: 1 ##Die Anzahl der verarbeiteten Verbindungen beträgt 1🎜Server ##Es wurden insgesamt 4 Verbindungen verarbeitet🎜akzeptiert ##Insgesamt Es wurden 4 Handshakes erstellt🎜verarbeitete Anfragen ##Es wurden insgesamt 4 Anfragen verarbeitet 🎜Lesen ##Die Anzahl der vom Client gelesenen Header-Informationen🎜Schreiben ##Die Anzahl der an den Client zurückgegebenen Header-Informationen🎜Warten ##NGinx hat die verarbeitet Anzahl der residenten Verbindungen, die auf den nächsten Anforderungsbefehl warten🎜🎜🎜4. Fehlerprotokoll hinzufügen 🎜🎜error_log-Syntax: 🎜🎜🎜error_log-Dateiebene; 🎜🎜🎜Schlüsselwörter bleiben unverändert, Datei ist der Protokollspeicherort, Ebene ist der Fehler Protokollebene🎜🎜Normalerweise werden nur drei Ebenen „warn|error|crit“ verwendet🎜🎜um das Fehlerprotokoll zu konfigurieren. Fügen Sie in der Konfiguration 🎜🎜🎜error_loglogs/error_log;🎜🎜🎜Ja, das stimmt, unter worker_processes 1;hinzu > in der Datei nging.conf! 🎜

Das obige ist der detaillierte Inhalt vonSo konfigurieren Sie allgemeine Funktionen nach der Nginx-Installation. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen