Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

安装

Docker 安全

  1. # 下载容器
  2. docker pull nginx:1.19.3
  3. # 运行容器
  4. docker run -d --name "nginx" -p 80:80 -p 443:443 nginx:1.19.3

Linux 安装

  1. # Centos
  2. yum install nginx
  3. # Debian
  4. apt install nginx

配置

目录说明

默认安装目录 /etc/nginx

image.png

mime.typs 文件后缀的映射 nginx.conf 配置文件的入口 conf.d 是http 子配置文件夹

image.png

注释基础配置

  1. # root@k8s-m:/etc/nginx# cat nginx.conf
  2. # 系统用户名
  3. user nginx;
  4. # 工作进程
  5. worker_processes 1;
  6. # 日志存放路径
  7. error_log /var/log/nginx/error.log warn;
  8. pid /var/run/nginx.pid;
  9. # 默认
  10. events {
  11. worker_connections 1024;
  12. }
  13. # http 的监听
  14. http {
  15. # 头文件
  16. include /etc/nginx/mime.types;
  17. # 默认的响应type
  18. default_type application/octet-stream;
  19. # 转发的headr
  20. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. '$status $body_bytes_sent "$http_referer" '
  22. '"$http_user_agent" "$http_x_forwarded_for"';
  23. # 请求日志
  24. access_log /var/log/nginx/access.log main;
  25. # sendfile实际上是 Linux2.0+以后的推出的一个系统调用
  26. # sendfile是个比 read 和 write 更高性能的系统接口
  27. sendfile on;
  28. # 第一次握手之后的超时时间;
  29. # 在此期间内后续的http请求可以复用已建立的TCP连接,从而节省新建TCP连接握手的时间与资源。
  30. keepalive_timeout 65;
  31. # 指定其他子目录的配置
  32. include /etc/nginx/conf.d/*.conf;
  33. }

前端资源配置 (Vue 虚拟路由)

  1. # root@k8s-m:/etc/nginx/conf.d# cat xinsj-admin.conf
  2. server {
  3. # 端口
  4. listen 80;
  5. # 域名, 空格分隔多个域名 xinsj-admin1.yuemia.com xinsj-admin2.yuemia.com
  6. server_name xinsj-admin.yuemia.com;
  7. # root 整个配置只能出现一次, 指定的跟目录, 其他目录可以用 alias
  8. root /html/xinsj-admin;
  9. # 默认页面
  10. index index.html index.htm;
  11. # 配置Vue 的虚拟路由
  12. try_files $uri $uri/ /index.html;
  13. # 启用压缩
  14. gzip on;
  15. # 资源大于1K才会压缩
  16. gzip_min_length 1k;
  17. # 设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间,param2:int(k) 后面单位是k。这里设置以16k为单位,按照原始数据大小以16k为单位的4倍申请内存
  18. gzip_buffers 4 16k;
  19. # 等级1-9 9最小的压缩,传输最快 但是消耗cpu
  20. gzip_comp_level 6;
  21. # 压缩的资源类型
  22. gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  23. # 启用应答头"Vary: Accept-Encoding", GZIP 压缩标记
  24. gzip_vary off;
  25. # IE6对Gzip不怎么友好,不给它Gzip了
  26. gzip_disable "MSIE [1-6]\.";
  27. }

后端反向代理

  1. # root@k8s-m:/etc/nginx/conf.d# cat xinsj.conf
  2. server {
  3. listen 80;
  4. server_name xinsj.yuemia.com;
  5. root /html/ypxsj;
  6. gzip on;
  7. gzip_min_length 1k;
  8. gzip_buffers 4 16k;
  9. gzip_comp_level 6;
  10. gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  11. gzip_vary off;
  12. gzip_disable "MSIE [1-6]\.";
  13. # 请求Body 大小
  14. client_max_body_size 200m;
  15. # 请求head 超时
  16. client_header_timeout 30m;
  17. # 请求与Body的超时
  18. client_body_timeout 30m;
  19. # 连接超时
  20. proxy_connect_timeout 360s;
  21. # 读取超时以及发送超时
  22. proxy_read_timeout 30m;
  23. proxy_send_timeout 30m;
  24. location /mobile/ {
  25. alias /html/xinsj/mobile/;
  26. index index.html index.htm;
  27. try_files $uri $uri/ /mobile/index.html;
  28. }
  29. location /WW_verify_hXpspD3AQMjrL6vY.txt {
  30. return 200 'hXpspD3AQMjrL6vY';
  31. }
  32. # 代理的路径
  33. location /api/ {
  34. # 代理地址, 如果这里加了 / ; 就是基于 /开始, 否则就是 /api 开始
  35. proxy_pass http://10.96.177.145:8033/;
  36. # 添加头部
  37. proxy_set_header Host $http_host;
  38. # 添加头部, 服务器IP
  39. proxy_set_header X-Real-IP $remote_addr;
  40. proxy_set_header X-Scheme $scheme;
  41. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  42. # Web Socket 支持
  43. proxy_set_header Upgrade $http_upgrade;
  44. proxy_set_header Connection "upgrade";
  45. proxy_http_version 1.1;
  46. # 连接超时的时间
  47. proxy_connect_timeout 4s;
  48. # 读取超时的时间
  49. proxy_read_timeout 900s;
  50. # 发送超时的时间
  51. proxy_send_timeout 12s;
  52. }
  53. }