Nginx服务器基础配置00

Nginx服务器基础配置实例

1.准备测试素材文件

image-20211116191534027

2.替换编写nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
user www-data;

worker_processes 2;

error_log /var/www/nginx_test/log/error.log;

daemon on;

events {
accept_mutex on;
multi_accept on;
worker_connections 1024;
use epoll;
}

http {
sendfile on;
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
log_format server1 '=====> server1 access log format';
log_format server2 '=====> server2 access log format';

include /etc/nginx/conf.d/conf.d/*.conf;

}

3.编写两个服务文件

server1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 8081;

server_name nginx.conf.test;

access_log /var/www/nginx_test/server1/logs/access.log server1;

location /server1/location1{
root /var/www/nginx_test;
index index_sr1_location1.html;
}

location /server1/location2{
root /var/www/nginx_test;
index index_sr1_location2.html;
}

error_page 404 /404.html;

location = /404.html{
root /var/www/nginx_test;
index 404.html;
}
}

server2.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 8082;

server_name nginx.conf.test;

access_log /var/www/nginx_test/server2/logs/access.log server2;

location /server2/location1{
root /var/www/nginx_test;
index index_sr2_location1.html;
}

location /server2/location2{
root /var/www/nginx_test;
index index_sr2_location2.html;
}

error_page 404 /404.html;

location /404.html{
root /var/www/nginx_test;
index 404.html;
}
}

4.重新加载启动nginx服务

1
sudo nginx -s reload

5.测试结果

image-20211116192129333

Nginx操作优化

Nginx配置系统服务

1
sudo vim /usr/lib/systemd/system/nginx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
[service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecstartPre=/usr/local/nginx/sbin/nginx -t -c /usr/loca1/nginx/conf/nginx.conf
Execstart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
Execstop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
1
sudo chmod 755 /usr/lib/systemd/system/nginx.service

使用系统命令执行nginx服务

1
2
3
4
5
6
systemctl start nginx  		# 启动
systemctl stop nginx # 停止
systemctl restart nginx # 重启
systemctl reload nginx # 重载配置
systemctl status nginx # 查看状态
systemctl enable nginx # 开机启动

本机测试用系统安装时已自行配置, 可直接使用systemctl操作nginx

Nginx命令配置到系统环境

1
sudo vim /etc/profile
1
2
最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
1
source /etc/profile

本机测试用系统安装时已自行配置, 可直接用nginx命令