1、nginx文件结构
... #全局块events { #events块 ...}http #http块{ ... #http全局块 server #server块 { ... #server全局块 location [PATTERN] #location块 { ... } location [PATTERN] { ... } } server { ... } ... #http全局块}
- 1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
- 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
- 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
- 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
- 5、location块:配置请求的路由,以及各种页面的处理情况。
2、常用调优
#常用调优 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #压缩 gzip on; gzip_comp_level 5; gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; #文件大小 client_max_body_size 300m; #SSL性能调优 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
3、静态http配置
server { listen 80; server_name localhost; client_max_body_size 1024M; location / { root /usr/.../; //通过/将所有的请求,转发给root的路径处理 index index.html; } location /h5/ { alias /usr/h5/.../; //通过/将所有的请求,转发给root的路径处理 index index.html; }}
4、反向代理与跨域配置
# 反向代理与跨域配置server { listen 80; server_name localhost; #访问localhost的时候相当于访问 http://localhost:8080 client_max_body_size 1024M; location / { # 设置跨域 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers *; # 代理到指定端口 proxy_pass http://xxx.xx.xx.xx:xxxx; # options 快速返回 204 if ($request_method = 'OPTIONS') { return 204; } }}
5、ssl配置
# ssl配置 ####################### git start ############################# server { listen 80; server_name git.zymapp.com; location / { return 301 https://git.zymapp.com; } } server { listen 443 ssl; server_name git.zymapp.com; ssl_certificate /usr/local/nginx/https/zymapp.com/fullchain.cer; ssl_certificate_key /usr/local/nginx/https/zymapp.com/private.key; ssl_trusted_certificate /usr/local/nginx/https/zymapp.com/ca.cer; location / { proxy_pass http://127.0.0.1:3000; } } ####################### git end #############################
6、nacos配置
############################################################ ###############--------nacos start--------################## ############################################################ server { listen 80; server_name seven.nacos.zymapp.com; location / { proxy_pass http://localhost:8848/nacos/; } } ###############--------nacos end--------################## ##########################################################
7、api网关集群配置
########################################################## ###############--------api start--------################## ########################################################## upstream gatewaycluster{ server 192.168.0.166:18001; server 192.168.0.94:18001; } server { listen 80; server_name seven.api.zymapp.com; location / { proxy_pass http://gatewaycluster/; } } ################--------api end--------################### ##########################################################
8、git配置
####################### git start ############################# #server { #listen 80; #server_name git.zymapp.com; #location / { #return 301 https://git.zymapp.com; #} #} #server { #listen 443 ssl; #server_name git.zymapp.com; #ssl_certificate /usr/local/nginx/https/zymapp.com/fullchain.cer; #ssl_certificate_key /usr/local/nginx/https/zymapp.com/private.key; #ssl_trusted_certificate /usr/local/nginx/https/zymapp.com/ca.cer; #location / { #proxy_pass http://127.0.0.1:3000; #} #} ####################### git end ############################# ######################## git start ########################## server { listen 80; server_name git.linzhi888.top; location / { proxy_pass http://127.0.0.1:3000; } } ######################## git end #############################
9、web网站配置
####################### kjcx start ############################# server { listen 80; server_name kjcx.zymapp.com; location / { return 301 https://$server_name$request_uri; } } server { listen 443 ssl; server_name kjcx.zymapp.com; ssl_certificate /usr/local/nginx/https/zymapp.com/fullchain.cer; ssl_certificate_key /usr/local/nginx/https/zymapp.com/private.key; ssl_trusted_certificate /usr/local/nginx/https/zymapp.com/ca.cer; try_files $uri $uri/ /index.html; root /home/official/kjcx; index index.html; } ####################### kjcx end #############################
####################### vue3-elementplus-admin-web start ############################# server { listen 1122; server_name 127.0.0.1:1122; try_files $uri $uri/ /index.html; root /home/sftp-root/vue3-admin-ftp/web; index index.html; } ####################### vue3-elementplus-admin-web end #############################