Nginx搭建web服务
# nginx.conf
- 主文件基本配置
error_log logs/error.log; #日志
# 启动进程,通常设置成和cpu的数量相等
worker_processes 2;
events {
worker_connections 1024; #最大连接数
}
# 保留最基本的简单配置即可
http {
access_log off;
keepalive_timeout 65; #超时时间
types_hash_max_size 2048;
include mime.types; #文件扩展名与类型映射表
include conf.d/*.conf; #引入并使用conf.d下的所有.conf配置
# sendfile on; #开启高效传输模式
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# conf.d/xx.conf
- 单独配置 server
server {
#外网
listen 443 ssl default_server; #端口 #listen 443 ssl http2 开启http2
server_name www.xxx.com; #域名或者ip
#https ssl加密
ssl_certificate cert/xxx.com.pem;
ssl_certificate_key cert/xxx.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# gzip setting
# 可以动静压缩结合,静态/动态都开启
gzip on;
gzip_min_length 1k; #开始压缩的最小长度(再小就不要压缩了,意义不在)
gzip_comp_level 6; #[1-9] 推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/xml text/javascript application/json;
gzip_static on; # 开启静态压缩,即直接使用已存在的gzip文件
gzip_http_version 1.0; #设置gzip压缩针对的HTTP协议版本
gzip_vary on; #是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_buffers 4 16k; #缓冲(压缩在内存中缓冲几块? 每块多大?)
# gzip_disable #正则匹配UA 什么样的Uri不进行gzip
# 监听本地服务
location / {
#本地静态文件
root /usr/local/app/dist; # 本地静态文件位置
index index.html index.htm; # 入口
#开启反向代理
proxy_pass http://127.0.0.1:3021/; #代理本地服务的 地址和端口号
#后端请求url配置
proxy_set_header Host $host;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header Accept-Encoding "";
}
}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 检查 nginx 配置
nginx 默认路径为 /usr/local/nginx ,如果在自定义其他位置,改变路径即可
启动:
/usr/local/nginx/sbin/nginx
停止:
/usr/local/nginx/sbin/nginx -s stop(quit)
重启:
/usr/local/nginx/sbin/nginx -s reload
1.检查配置:
/usr/local/nginx/sbin/nginx -t
2.重启:
/usr/local/nginx/sbin/nginx -s reload
3.查看进程:
ps -ef | grep nginx
//ps -ef 查看进程列表, | grep4.显示 tcp,udp 的端口和进程等相关情况:
netstat -tunlp | grep
端口号
# 检查 nginx.conf 是否成功,nginx -t 只会检查默认路径的
- nginx 项目下直接
nginx -t
其他任何位置nginx -t -c /home/admin/nginx/nginx.conf
# 重启配置
- nginx 项目下直接
nginx -s
- 其他任何位置 /usr/local/openresty/nginx/sbin/nginx -p
pwd
/ -c nginx.conf -s reload
上次更新: 2022/12/07, 14:14:50