Nginx配置文件nginx.conf全解

绝地灬酷狼 2023-06-26 05:58 70阅读 0赞

Nginx配置文件nginx.conf全解

nginx配置文件nginx.conf的配置http、upstream、server、location等;

nginx负载均衡算法:轮询、加权轮询、ip_hash、url_hash等策略配置;

nginx日志文件access_log配置;

代理服务缓存proxy_buffer设置。

  1. #user nobody; # 用户 用户组 ;一般只有类unix系统有,windows不需要
  2. worker_processes 1; # 工作进程,一般配置为cpu核心数的2倍
  3. # 错误日志的配置路径
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. # 进程id存放路径
  8. #pid logs/nginx.pid;
  9. # 指定进程可以打开的最大描述符的数量
  10. # 整个系统的最大文件描述符打开数目可以通过命令 ulimit -n 查看
  11. # 所以一般而言,这个值可以是 ulimit -n 除以nginx进程数
  12. # worker_rlimit_nofile 65535;
  13. events {
  14. # 使用epoll的I/O 模型;epoll 使用于Linux内核2.6版本及以后的系统
  15. use epoll;
  16. # 每一个工作进程的最大连接数量; 理论上而言每一台nginx服务器的最大连接数为: worker_processes*worker_connections
  17. worker_connections 1024;
  18. # 超时时间
  19. #keepalive_timeout 60
  20. # 客户端请求头部的缓冲区大小,客户端请求一般会小于一页; 可以根据你的系统的分页大小来设定, 命令 getconf PAGESIZE 可以获得当前系统的分页大小(一般4K)
  21. #client_header_buffer_size 4k;
  22. # 为打开的文件指定缓存,默认是不启用; max指定缓存数量,建议和打开文件数一致;inactive是指经过这个时间后还没有被请求过则清除该文件的缓存。
  23. #open_file_cache max=65535 inactive=60s;
  24. # 多久会检查一次缓存的有效信息
  25. #open_file_cache_valid 80s;
  26. # 如果在指定的参数open_file_cache的属性inactive设置的值之内,没有被访问这么多次(open_file_cache_min_uses),则清除缓存
  27. # 则这里指的是 60s内都没有被访问过一次则清除 的意思
  28. # open_file_cache_min_uses 1;
  29. }
  30. # 设定http服务;可以利用它的反向代理功能提供负载均衡支持
  31. http {
  32. #设定mime类型,类型由mime.types文件定义; 可以 cat nginx/conf/mime.types 查看下支持哪些类型
  33. include mime.types;
  34. # 默认mime类型; application/octet-stream 指的是原始二进制流
  35. default_type application/octet-stream;
  36. # -------------------------------------------------------------------------- #
  37. # 日志格式设置:
  38. # $remote_addr、$http_x_forwarded_for 可以获得客户端ip地址
  39. # $remote_user 可以获得客户端用户名
  40. # $time_local 记录访问的时区以及时间
  41. # $request 请求的url与http协议
  42. # $status 响应状态成功为200
  43. # $body_bytes_sent 发送给客户端主体内容大小
  44. # $http_referer 记录从哪个页面过来的请求
  45. # $http_user_agent 客户端浏览器信息
  46. #
  47. # 注意事项:
  48. # 通常web服务器(我们的tomcat)放在反向代理(nginx)的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。
  49. # 反向代理服务器(nginx)在转发请求的http头信息中,可以增加$http_x_forwarded_for信息,记录原有客户端的IP地址和原来客户端的请求的服务器地址。
  50. # -------------------------------------------------------------------------- #
  51. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  52. # '$status $body_bytes_sent "$http_referer" '
  53. # '"$http_user_agent" "$http_x_forwarded_for"';
  54. #log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';
  55. # 日志文件; 【注意:】用了log_format则必须指定access_log来指定日志文件
  56. #access_log logs/access.log main;
  57. #access_log logs/access.404.log log404;
  58. # 保存服务器名字的hash表由 server_names_hash_bucket_size、server_names_hash_max_size 控制
  59. # server_names_hash_bucket_size 128;
  60. # 限制通过nginx上传文件的大小
  61. #client_max_body_size 300m;
  62. # sendfile 指定 nginx 是否调用sendfile 函数(零拷贝 方式)来输出文件;
  63. # 对于一般常见应用,必须设为on。
  64. # 如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
  65. sendfile on;
  66. # 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用;TCP_CORK TCP_NODELAY 选项可以是否打开TCP的内格尔算法
  67. #tcp_nopush on;
  68. #tcp_nodelay on;
  69. # 后端服务器连接的超时时间_发起握手等候响应超时时间
  70. #proxy_connect_timeout 90;
  71. # 连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
  72. #proxy_read_timeout 180;
  73. # 后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
  74. #proxy_send_timeout 180;
  75. # proxy_buffering 这个参数用来控制是否打开后端响应内容的缓冲区,如果这个设置为on,以下的proxy_buffers才生效
  76. #proxy_buffering on
  77. # 设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,
  78. # 默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
  79. #proxy_buffer_size 256k;
  80. # 设置用于读取应答(来自被代理服务器--如tomcat)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
  81. #proxy_buffers 4 256k;
  82. # 同一时间处理的请求buffer大小;也可以说是一个最大的限制值--控制同时传输到客户端的buffer大小的。
  83. #proxy_busy_buffers_size 256k;
  84. # 设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
  85. #proxy_temp_file_write_size 256k;
  86. # proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
  87. #proxy_temp_path /app/tmp/proxy_temp_dir;
  88. # 设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为10GB。
  89. #proxy_cache_path /app/tmp/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  90. # 如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。
  91. # 如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
  92. # 无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误
  93. #client_body_buffer_size 512k; # 默认是页大小的两倍
  94. # 表示使nginx阻止HTTP应答代码为400或者更高的应答。可以结合error_page指向特定的错误页面展示错误信息
  95. #proxy_intercept_errors on;
  96. #keepalive_timeout 0;
  97. keepalive_timeout 65;
  98. #gzip on;
  99. # 负载均衡 START>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  100. # upstream 指令定义的节点可以被proxy_pass指令引用;二者结合用来反向代理+负载均衡配置
  101. # 【内置策略】:轮询、加权轮询、ip_hash、最少连接 默认编译进了nginx
  102. # 【扩展策略】:fair、通用hash、一致性hash 默认没有编译进nginx
  103. #-----------------------------------------------------------------------------------------------#
  104. # 【1】默认是轮询;如果后端服务器down掉,能自动剔除。
  105. # upstream bakend {
  106. # server 192.168.75.130:8080;
  107. # server 192.168.75.132:8080;
  108. # server 192.168.75.134:8080;
  109. # }
  110. #
  111. #【2】权重轮询(加权轮询):这样配置后,如果总共请求了3次,则前面两次请求到130,后面一次请求到132
  112. # upstream bakend {
  113. # server 192.168.75.130:8080 weight=2;
  114. # server 192.168.75.132:8080 weight=1;
  115. # }
  116. #
  117. #【3】ip_hash:这种配置会使得每个请求按访问者的ip的hash结果分配,这样每个访客固定访问一个后端服务器,这样也可以解决session的问题。
  118. # upstream bakend {
  119. # ip_hash;
  120. # server 192.168.75.130:8080;
  121. # server 192.168.75.132:8080;
  122. # }
  123. #
  124. #【4】最少连接:将请求分配给连接数最少的服务器。Nginx会统计哪些服务器的连接数最少。
  125. # upstream bakend {
  126. # least_conn;
  127. # server 192.168.75.130:8080;
  128. # server 192.168.75.132:8080;
  129. # }
  130. #
  131. #
  132. #【5】fair策略(需要安装nginx的第三方模块fair):按后端服务器的响应时间来分配请求,响应时间短的优先分配。
  133. # upstream bakend {
  134. # fair;
  135. # server 192.168.75.130:8080;
  136. # server 192.168.75.132:8080;
  137. # }
  138. #
  139. #【6】url_hash策略(也是第三方策略):按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
  140. # 在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method指定hash算法
  141. # upstream bakend {
  142. # server 192.168.75.130:8080;
  143. # server 192.168.75.132:8080;
  144. # hash $request_uri;
  145. # hash_method crc32;
  146. # }
  147. #
  148. #【7】其他设置,主要是设备的状态设置
  149. # upstream bakend{
  150. # ip_hash;
  151. # server 127.0.0.1:9090 down; # down 表示该机器处于下线状态不可用
  152. # server 127.0.0.1:8080 weight=2;
  153. # server 127.0.0.1:6060;
  154. #
  155. # # max_fails 默认为1; 最大请求失败的次数,结合fail_timeout使用;
  156. # # 以下配置表示 192.168.0.100:8080在处理请求失败3次后,将在15s内不会受到任何请求了
  157. # # fail_timeout 默认为10秒。某台Server达到max_fails次失败请求后,在fail_timeout期间内,nginx会认为这台Server暂时不可用,不会将请求分配给它。
  158. # server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
  159. # server 192.168.0.101:8080 weight=3;
  160. # server 192.168.0.102:8080 weight=1;
  161. # # 限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置
  162. # server 192.168.0.103:8080 max_conns=1000;
  163. # server 127.0.0.1:7070 backup; # 备份机;其他机器都不可用时,这台机器就上场了
  164. # server example.com my_dns_resolve; # 指定域名解析器;my_dns_resolve需要在http节点配置resolver节点如:resolver 10.0.0.1;
  165. # }
  166. #
  167. #
  168. #
  169. #负载均衡 END<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  170. #-----------------------------------------------------------------------------------------------#
  171. ###配置虚拟机START>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  172. #
  173. #server{
  174. # listen 80;
  175. # 配置监听端口
  176. # server_name image.***.com;
  177. # 配置访问域名
  178. # location ~* \.(mp3|exe)$ {
  179. # 对以“mp3或exe”结尾的地址进行负载均衡
  180. # proxy_pass http://img_relay$request_uri;
  181. # 设置被代理服务器的端口或套接字,以及URL
  182. # proxy_set_header Host $host;
  183. # proxy_set_header X-Real-IP $remote_addr;
  184. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  185. # 以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
  186. # }
  187. #
  188. #
  189. #
  190. # location /face {
  191. # if ($http_user_agent ~* "xnp") {
  192. # rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  193. # }
  194. # proxy_pass http://img_relay$request_uri;
  195. # proxy_set_header Host $host;
  196. # proxy_set_header X-Real-IP $remote_addr;
  197. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  198. # error_page 404 502 = @fetch;
  199. # }
  200. #
  201. # location @fetch {
  202. # access_log /data/logs/face.log log404;
  203. # rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  204. # }
  205. #
  206. # location /image {
  207. # if ($http_user_agent ~* "xnp") {
  208. # rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  209. #
  210. # }
  211. # proxy_pass http://img_relay$request_uri;
  212. # proxy_set_header Host $host;
  213. # proxy_set_header X-Real-IP $remote_addr;
  214. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  215. # error_page 404 502 = @fetch;
  216. # }
  217. #
  218. # location @fetch {
  219. # access_log /data/logs/image.log log404;
  220. # rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  221. # }
  222. #}
  223. #
  224. #
  225. #
  226. ###其他举例
  227. #
  228. #server{
  229. #
  230. # listen 80;
  231. #
  232. # server_name *.***.com *.***.cn;
  233. #
  234. # location ~* \.(mp3|exe)$ {
  235. # proxy_pass http://img_relay$request_uri;
  236. # proxy_set_header Host $host;
  237. # proxy_set_header X-Real-IP $remote_addr;
  238. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  239. # }
  240. #
  241. # location / {
  242. # if ($http_user_agent ~* "xnp") {
  243. # rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  244. # }
  245. #
  246. # proxy_pass http://img_relay$request_uri;
  247. # proxy_set_header Host $host;
  248. # proxy_set_header X-Real-IP $remote_addr;
  249. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  250. # #error_page 404 http://i1.***img.com/help/noimg.gif;
  251. # error_page 404 502 = @fetch;
  252. # }
  253. #
  254. # location @fetch {
  255. # access_log /data/logs/baijiaqi.log log404;
  256. # rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  257. # }
  258. #}
  259. #
  260. #server{
  261. # listen 80;
  262. # server_name *.***img.com;
  263. #
  264. # location ~* \.(mp3|exe)$ {
  265. # proxy_pass http://img_relay$request_uri;
  266. # proxy_set_header Host $host;
  267. # proxy_set_header X-Real-IP $remote_addr;
  268. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  269. # }
  270. #
  271. # location / {
  272. # if ($http_user_agent ~* "xnp") {
  273. # rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;
  274. # }
  275. #
  276. # proxy_pass http://img_relay$request_uri;
  277. # proxy_set_header Host $host;
  278. # proxy_set_header X-Real-IP $remote_addr;
  279. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  280. # #error_page 404 http://i1.***img.com/help/noimg.gif;
  281. # error_page 404 = @fetch;
  282. # }
  283. #
  284. ##access_log off;
  285. #
  286. # location @fetch {
  287. # access_log /data/logs/baijiaqi.log log404;
  288. # rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  289. # }
  290. #}
  291. #
  292. #server{
  293. # listen 8080;
  294. # server_name ngx-ha.***img.com;
  295. #
  296. # location / {
  297. # stub_status on;
  298. # access_log off;
  299. # }
  300. #}
  301. #
  302. #server {
  303. # listen 80;
  304. # server_name imgsrc1.***.net;
  305. # root html;
  306. #}
  307. #
  308. #
  309. #
  310. #server {
  311. # listen 80;
  312. # server_name ***.com w.***.com;
  313. # # access_log /usr/local/nginx/logs/access_log main;
  314. # location / {
  315. # rewrite ^(.*)$ http://www.***.com/ ;
  316. # }
  317. #}
  318. #
  319. #server {
  320. # listen 80;
  321. # server_name *******.com w.*******.com;
  322. # # access_log /usr/local/nginx/logs/access_log main;
  323. # location / {
  324. # rewrite ^(.*)$ http://www.*******.com/;
  325. # }
  326. #}
  327. #
  328. #server {
  329. # listen 80;
  330. # server_name ******.com;
  331. #
  332. # # access_log /usr/local/nginx/logs/access_log main;
  333. #
  334. # location / {
  335. # rewrite ^(.*)$ http://www.******.com/;
  336. # }
  337. #
  338. # location /NginxStatus {
  339. # stub_status on;
  340. # access_log on;
  341. # auth_basic "NginxStatus";
  342. # auth_basic_user_file conf/htpasswd;
  343. # }
  344. #
  345. # #设定查看Nginx状态的地址
  346. # location ~ /\.ht {
  347. # deny all;
  348. # }#禁止访问.htxxx文件
  349. #
  350. #}
  351. #配置虚拟机END<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  352. server {
  353. listen 80;
  354. server_name localhost;
  355. #charset koi8-r;
  356. #access_log logs/host.access.log main;
  357. location / {
  358. #root html;
  359. root /app;
  360. index zyt505050.html;
  361. #index index.html index.htm;
  362. }
  363. #error_page 404 /404.html;
  364. # redirect server error pages to the static page /50x.html
  365. #
  366. error_page 500 502 503 504 /50x.html;
  367. location = /50x.html {
  368. root html;
  369. }
  370. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  371. #
  372. #location ~ \.php$ {
  373. # proxy_pass http://127.0.0.1;
  374. #}
  375. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  376. #
  377. #location ~ \.php$ {
  378. # root html;
  379. # fastcgi_pass 127.0.0.1:9000;
  380. # fastcgi_index index.php;
  381. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  382. # include fastcgi_params;
  383. #}
  384. # deny access to .htaccess files, if Apache's document root
  385. # concurs with nginx's one
  386. #
  387. #location ~ /\.ht {
  388. # deny all;
  389. #}
  390. }
  391. # another virtual host using mix of IP-, name-, and port-based configuration
  392. #
  393. #server {
  394. # listen 8000;
  395. # listen somename:8080;
  396. # server_name somename alias another.alias;
  397. # location / {
  398. # root html;
  399. # index index.html index.htm;
  400. # }
  401. #}
  402. # HTTPS server
  403. #
  404. #server {
  405. # listen 443 ssl;
  406. # server_name localhost;
  407. # ssl_certificate cert.pem;
  408. # ssl_certificate_key cert.key;
  409. # ssl_session_cache shared:SSL:1m;
  410. # ssl_session_timeout 5m;
  411. # ssl_ciphers HIGH:!aNULL:!MD5;
  412. # ssl_prefer_server_ciphers on;
  413. # location / {
  414. # root html;
  415. # index index.html index.htm;
  416. # }
  417. #}
  418. }

发表评论

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

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

相关阅读

    相关 nginxconf文件下载

    如何编辑nginx.conf 文件 nginx.conf文件为文本类型的文件,可以用任何文本编辑器进行编辑 注意,在windows下,如果使用记事本进行编辑,文件中不能