nginx配置文件

╰+攻爆jí腚メ 2022-12-19 14:27 266阅读 0赞

nginx配置文件

    • 全局配置
    • 事件配置
    • http参数
    • 虚拟主机基本配置
    • 反向代理
    • 负载均衡
      • 反向代理使用
      • 反向代理+负载均衡使用

全局配置

  1. #Nginx的worker进程运行用户以及用户组
  2. #user nobody;
  3. #Nginx开启的进程数
  4. worker_processes 1;
  5. #以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.
  6. #这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。eg:
  7. #worker_processes 4 #4核CPU
  8. #worker_cpu_affinity 0001 0010 0100 1000;
  9. #定义全局错误日志定义类型,[debug|info|notice|warn|crit]
  10. #error_log logs/error.log;
  11. #error_log logs/error.log notice;
  12. #error_log logs/error.log info;
  13. #指定进程ID存储文件位置
  14. #pid logs/nginx.pid;

   

事件配置

  1. #每个进程可以处理的最大连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。理论值:worker_rlimit_nofile/worker_processes
  2. #注意:最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处
  3. events {
  4. worker_connections 1024;
  5. }
  6. #单个进程最大连接数(最大连接数=连接数*进程数)

   

http参数

  1. #文件扩展名与文件类型映射表
  2. include mime.types;
  3. #默认文件类型
  4. default_type application/octet-stream;
  5. #日志相关定义
  6. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  7. # '$status $body_bytes_sent "$http_referer" '
  8. # '"$http_user_agent" "$http_x_forwarded_for"';
  9. #access_log logs/access.log main;
  10. #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
  11. #2.$remote_user :用来记录客户端用户名称;
  12. #3.$time_local :用来记录访问时间与时区;
  13. #4.$request :用来记录请求的url与http协议;
  14. #5.$status :用来记录请求状态;
  15. #6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
  16. #7.$http_referer :用来记录从那个页面链接访问过来的;
  17. #8.$http_user_agent :记录客户端浏览器的相关信息
  18. #连接日志的路径,指定的日志格式放在最后。
  19. #access_log logs/access.log main;
  20. #只记录更为严重的错误日志,减少IO压力
  21. error_log logs/error.log crit;
  22. #关闭日志
  23. #access_log off;
  24. #默认编码
  25. #charset utf-8;
  26. #服务器名字的hash表大小
  27. server_names_hash_bucket_size 128;
  28. #客户端请求单个文件的最大字节数
  29. client_max_body_size 8m;
  30. #指定来自客户端请求头的hearerbuffer大小
  31. client_header_buffer_size 32k;
  32. #指定客户端请求中较大的消息头的缓存最大数量和大小。
  33. large_client_header_buffers 4 64k;
  34. #开启高效传输模式。
  35. sendfile on;
  36. #防止网络阻塞
  37. #tcp_nopush on;
  38. #客户端连接超时时间,单位是秒
  39. #keepalive_timeout 0;
  40. #客户端请求头读取超时时间
  41. client_header_timeout 10;
  42. #设置客户端请求主体读取超时时间
  43. client_body_timeout 10;
  44. #响应客户端超时时间
  45. send_timeout 10;
  46. FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
  47. fastcgi_connect_timeout 300;
  48. fastcgi_send_timeout 300;
  49. fastcgi_read_timeout 300;
  50. fastcgi_buffer_size 64k;
  51. fastcgi_buffers 4 64k;
  52. fastcgi_busy_buffers_size 128k;
  53. fastcgi_temp_file_write_size 128k;
  54. #gzip模块设置
  55. #开启gzip压缩输出
  56. #gzip on;
  57. #最小压缩文件大小
  58. gzip_min_length 1k;
  59. #压缩缓冲区
  60. gzip_buffers 4 16k;
  61. #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
  62. gzip_http_version 1.0;
  63. #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
  64. gzip_comp_level 2;
  65. #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
  66. gzip_types text/plain application/x-javascript text/css application/xml;
  67. #前端缓存服务器缓存经过压缩的页面
  68. gzip_vary on;

   

虚拟主机基本配置

  1. #虚拟主机定义
  2. server {
  3. #监听端口
  4. listen 80;
  5. #访问域名
  6. server_name localhost;
  7. #编码格式,若网页格式与此不同,将被自动转码
  8. #charset koi8-r;
  9. #虚拟主机访问日志定义
  10. #access_log logs/host.access.log main;
  11. #对URL进行匹配
  12. location / {
  13. #访问路径,可相对也可绝对路径
  14. root html;
  15. #首页文件。以下按顺序匹配
  16. index index.html index.htm;
  17. }
  18. #错误信息返回页面
  19. #error_page 404 /404.html;
  20. # redirect server error pages to the static page /50x.html
  21. #
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. #访问URL以.php结尾则自动转交给127.0.0.1
  27. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  28. #
  29. #location ~ \.php$ {
  30. # proxy_pass http://127.0.0.1;
  31. #}
  32. #php脚本请求全部转发给FastCGI处理
  33. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  34. #
  35. #location ~ \.php$ {
  36. # root html;
  37. # fastcgi_pass 127.0.0.1:9000;
  38. # fastcgi_index index.php;
  39. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  40. # include fastcgi_params;
  41. #}
  42. #禁止访问.ht页面 (需ngx_http_access_module模块)
  43. # deny access to .htaccess files, if Apache's document root
  44. # concurs with nginx's one
  45. #
  46. #location ~ /\.ht {
  47. # deny all;
  48. #}
  49. }
  50. # another virtual host using mix of IP-, name-, and port-based configuration
  51. #
  52. #server {
  53. # listen 8000;
  54. # listen somename:8080;
  55. # server_name somename alias another.alias;
  56. # location / {
  57. # root html;
  58. # index index.html index.htm;
  59. # }
  60. #}
  61. #HTTPS虚拟主机定义
  62. # HTTPS server
  63. #
  64. #server {
  65. # listen 443 ssl;
  66. # server_name localhost;
  67. # ssl_certificate cert.pem;
  68. # ssl_certificate_key cert.key;
  69. # ssl_session_cache shared:SSL:1m;
  70. # ssl_session_timeout 5m;
  71. # ssl_ciphers HIGH:!aNULL:!MD5;
  72. # ssl_prefer_server_ciphers on;
  73. # location / {
  74. # root html;
  75. # index index.html index.htm;
  76. # }
  77. #}
  78. #vue配置
  79. server {
  80. listen 80;
  81. server_name jcsd-cdn-monitor.jdcloud.com;
  82. #charset koi8-r;
  83. #access_log logs/host.access.log main;
  84. root /root/dist;
  85. location / {
  86. try_files $uri $uri/ /index.html;
  87. }
  88. error_page 500 502 503 504 /50x.html;
  89. location = /50x.html {
  90. root html;
  91. }
  92. }
  93. }

   

反向代理

  1. #以下配置追加在HTTP的全局变量中
  2. #启动代理缓存功能
  3. proxy_buffering on;
  4. #nginx跟后端服务器连接超时时间(代理连接超时)
  5. proxy_connect_timeout 5;
  6. #后端服务器数据回传时间(代理发送超时)
  7. proxy_send_timeout 5;
  8. #连接成功后,后端服务器响应时间(代理接收超时)
  9. proxy_read_timeout 60;
  10. #设置代理服务器(nginx)保存用户头信息的缓冲区大小
  11. proxy_buffer_size 16k;
  12. #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
  13. proxy_buffers 4 32k;
  14. #高负荷下缓冲大小(proxy_buffers*2)
  15. proxy_busy_buffers_size 64k;
  16. #设定缓存文件夹大小,大于这个值,将从upstream服务器传
  17. proxy_temp_file_write_size 64k;
  18. #反向代理缓存目录
  19. proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
  20. #levels=1:2 设置目录深度,第一层目录是1个字符,第2层是2个字符
  21. #keys_zone:设置web缓存名称和内存缓存空间大小
  22. #inactive:自动清除缓存文件时间。
  23. #max_size:硬盘空间最大可使用值。
  24. #指定临时缓存文件的存储路径(必须在同一分区)
  25. proxy_temp_path /data/proxy/temp;
  26. #服务配置
  27. server {
  28. #侦听的80端口
  29. listen 80;
  30. server_name localhost;
  31. location / {
  32. #反向代理缓存设置命令(proxy_cache zone|off,默认关闭所以要设置)
  33. proxy_cache cache_one;
  34. #对不同的状态码缓存不同时间
  35. proxy_cache_valid 200 304 12h;
  36. #设置以什么样参数获取缓存文件名
  37. proxy_cache_key $host$uri$is_args$args;
  38. #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
  39. proxy_set_header Host $host;
  40. proxy_set_header X-Real-IP $remote_addr;
  41. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  42. #代理设置
  43. proxy_pass http://IP;
  44. #文件过期时间控制
  45. expires 1d;
  46. }
  47. #配置手动清楚缓存(实现此功能需第三方模块 ngx_cache_purge)
  48. #http://www.123.com/2017/0316/17.html访问
  49. #http://www.123.com/purge/2017/0316/17.html清楚URL缓存
  50. location ~ /purge(/.*) {
  51. allow 127.0.0.1;
  52. deny all;
  53. proxy_cache_purge cache_one $host$1$is_args$args;
  54. }
  55. #设置扩展名以.jsp、.php、.jspx结尾的动态应用程序不做缓存
  56. location ~.*\.(jsp|php|jspx)?$ {
  57. proxy_set_header Host $host;
  58. proxy_set_header X-Real-IP $remote_addr;
  59. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  60. proxy_pass http://IP;
  61. }

   

负载均衡

  1. #负载均衡服务器池
  2. upstream my_server_pool {
  3. #调度算法
  4. #1.轮循(默认)(weight轮循权值)
  5. #2 Weight:指定轮询权值,Weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下;
  6. #3.ip_hash:根据每个请求访问IP的hash结果分配。(会话保持)
  7. #4.fair:根据后端服务器响应时间最短请求。(upstream_fair模块)
  8. #5.url_hash:根据访问的url的hash结果分配。(需hash软件包)
  9. #参数:
  10. #down:表示不参与负载均衡
  11. #backup:备份服务器
  12. #max_fails:允许最大请求错误次数
  13. #fail_timeout:请求失败后暂停服务时间。
  14. server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
  15. server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
  16. }
  17. #负载均衡调用
  18. server {
  19. ...
  20. location / {
  21. proxy_pass http://my_server_pool;
  22. }
  23. }

反向代理使用

  1. upstream tomcatserver1 {
  2. server 192.168.72.49:8081;
  3. }
  4. upstream tomcatserver2 {
  5. server 192.168.72.49:8082;
  6. }
  7. server {
  8. listen 80;
  9. server_name 8081.max.com;
  10. #charset koi8-r;
  11. #access_log logs/host.access.log main;
  12. location / {
  13. proxy_pass http://tomcatserver1;
  14. index index.html index.htm;
  15. }
  16. }
  17. server {
  18. listen 80;
  19. server_name 8082.max.com;
  20. #charset koi8-r;
  21. #access_log logs/host.access.log main;
  22. location / {
  23. proxy_pass http://tomcatserver2;
  24. index index.html index.htm;
  25. }
  26. }

反向代理+负载均衡使用

  1. upstream zyy {
  2. server 192.168.30.245:80 weight=1;
  3. server 192.168.30.246:80 weight=1;
  4. }
  5. server {
  6. listen 80;
  7. server_name localhost;
  8. location / {
  9. proxy_pass http://zyy;
  10. }

发表评论

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

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

相关阅读

    相关 nginx 配置文件配置

    nginx 分为三个模块 核心模块 : nginx最基本最核心的服务,例如 进程管理,权限控制,日志记录 http模块 第三方模块   nginx配置文件实例

    相关 Nginx配置文件

    Nginx配置文件 一、组成部分 1. main:全局设置 2. server:主机设置 3. upstream:上游服务器设置【反向代理、负载均衡】 4.