Nginx系列之-基础配置详解

曾经终败给现在 2022-12-19 08:39 167阅读 0赞

文章目录

  • 前言
  • 一、Nginx.conf
  • 二、个人使用配置
    • 1、nginx.conf配置
    • 2、conf.d/文件夹配置文件
  • 总结

前言

之前也零零碎碎使用过Nginx,以及实现过最基础的配置,以及成功运运行,但是没有真正系统的使用,自从成为架构师以后,公司没有运维,什么都要自己捣鼓,又担心自己做的不够细,不够严谨,出现问题,造成损失,所以不断花时间巩固之气那自己锁涉及的技术知识点,而不是仅仅会用了。


提示:以下是本篇文章正文内容,下面案例可供参考

一、Nginx.conf

Nginx的安装我先不说了,非常的简单一条简单的sudo apt-get install nginx即可。下面我从:cd /etc/nginx路径copy的Nginx.conf我查查资料做个详细的说明输出。

  1. user nginx;
  2. worker_processes 4;
  3. pid /run/nginx.pid;
  4. events {
  5. worker_connections 2800;
  6. }
  7. http {
  8. ##
  9. # Basic Settings
  10. ##
  11. sendfile on;
  12. tcp_nopush on;
  13. tcp_nodelay on;
  14. keepalive_timeout 65;
  15. types_hash_max_size 2048;
  16. include /etc/nginx/mime.types;
  17. default_type application/octet-stream;
  18. ##
  19. # Logging Settings
  20. ##
  21. access_log /var/log/nginx/access.log;
  22. error_log /var/log/nginx/error.log;
  23. ##
  24. # Gzip Settings
  25. ##
  26. gzip on;
  27. include /etc/nginx/conf.d/*.conf;
  28. }
  1. user nginx; #用户
  2. worker_processes 4; #初始可设置为CPU总核数
  3. pid /run/nginx.pid; #用于管理nginx进程
  4. worker_connections 2800; #单个进程最大连接数(最大连接数=连接数*进程数)
  5. sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
  6. tcp_nopush on; #防止网络阻塞
  7. tcp_nodelay on; #防止网络阻塞
  8. keepalive_timeout 120; #长连接超时时间,单位是秒
  9. types_hash_max_size 影响散列表的冲突率。types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
  10. include mime.types; #文件扩展名与文件类型映射表
  11. default_type application/octet-stream; #默认文件类型
  12. access_log 主机的访问日志
  13. error_log 错误日志
  14. gzip on; #开启gzip压缩输出
  15. include /etc/nginx/conf.d/*.conf; #后续的配置我基本都会放在这个文件夹

二、个人使用配置

1、nginx.conf配置

  1. user nginx;
  2. worker_processes 4;
  3. pid /var/run/nginx.pid;
  4. #pid /run/nginx.pid
  5. events {
  6. worker_connections 1024;
  7. use epoll;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. access_log /var/log/nginx/access.log main;
  16. error_log /var/log/nginx/error.log warn;
  17. sendfile on;
  18. client_max_body_size 2048m;
  19. keepalive_timeout 65;
  20. underscores_in_headers on;
  21. #add_header Access-Control-Allow-Origin *;
  22. #add_header Access-Control-Allow-Headers X-Requested-With;
  23. #add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  24. include /etc/nginx/conf.d/*.conf; upstream whyt { server 192.168.0.199:8080; } }

2、conf.d/文件夹配置文件

在这里插入图片描述配置文件:whyt-gateway.conf

  1. server {
  2. listen 60080;
  3. #server_name localhost;
  4. server_name 外网ip;
  5. # zuul
  6. location /whyt-gateway/ {
  7. rewrite /whyt-gateway/(.*) /$1 break;
  8. proxy_pass http://whyt;
  9. #add_header Access-Control-Allow-Origin *;
  10. #add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
  11. #add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. }
  14. # whyt-backend
  15. location / {
  16. root /opt/nginx/whyt-backend/dist;
  17. #try_files $uri $uri/ @router;
  18. try_files $uri $uri/ /index.html last
  19. index index.html;
  20. }
  21. location /prod-api/ {
  22. proxy_set_header Host $http_host;
  23. proxy_set_header X-Real-IP $remote_addr;
  24. proxy_set_header REMOTE-HOST $remote_addr;
  25. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  26. proxy_pass http://localhost:8080/;
  27. }
  28. error_page 500 502 503 504 /50x.html;
  29. location = /50x.html {
  30. root /usr/share/nginx/html;
  31. }
  32. }

总结

配置不做详细讲解,我这边就暴漏了两个路由一个业务Api一个后台管理系统Api。

发表评论

表情:
评论列表 (有 0 条评论,167人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx配置详解

    [Nginx配置详解][Nginx] 序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。从2004年发布至今,凭借开源