温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx中怎么配置静态文件

发布时间:2021-07-19 15:21:13 来源:亿速云 阅读:294 作者:Leah 栏目:web开发

本篇文章为大家展示了Nginx中怎么配置静态文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

  1. #######################################################  

  2. ### Calomel.org /etc/Nginx.conf BEGIN  

  3. #######################################################  

  4. #  

  5. pid /var/run/Nginx.pid;  

  6. user Nginx Nginx;  

  7. worker_processes 2;  

  8. events {  

  9. worker_connections 1024;  

  10. }  

  11. http {  

  12. ## MIME types  

  13. include mime.types;  

  14. # types {  

  15. # image/gif gif;  

  16. # image/jpeg jpg;  

  17. # image/png png;  

  18. # image/bmp bmp;  

  19. # image/x-icon ico;  

  20. # text/css css;  

  21. # text/html html;  

  22. # text/plain bob;  

  23. # text/plain txt;  

  24. }  

  25. default_type application/octet-stream;  

  26. ## Size Limits  

  27. client_body_buffer_size 8k;  

  28. client_header_buffer_size 1k;  

  29. client_max_body_size 1k;  

  30. large_client_header_buffers 1 1k;  

  31. ## Timeouts   

  32. client_body_timeout 5;  

  33. client_header_timeout 5;  

  34. keepalive_timeout 5 5;  

  35. send_timeout 5;  

  36. ## General Options  

  37. ignore_invalid_headers on;  

  38. limit_zone gulag $binary_remote_addr 1m;  

  39. recursive_error_pages on;  

  40. sendfile on;  

  41. server_name_in_redirect off;  

  42. server_tokens off;  

  43. ## TCP options   

  44. tcp_nodelay on;  

  45. tcp_nopush on;  

  46. ## Compression  

  47. gzip on;  

  48. gzip_static on;  

  49. gzip_buffers 16 8k;  

  50. gzip_comp_level 9;  

  51. gzip_http_version 1.0;  

  52. gzip_min_length 0;  

  53. gzip_types text/plain text/html text/css image/x-icon image/
    bmp;  

  54. gzip_vary on;  

  55. ## Log Format  

  56. log_format main '$remote_addr $host $remote_user [$time_
    local] "$request" '  

  57. '$status $body_bytes_sent "$http_referer" "$http_user_
    agent" "$gzip_ratio"';  

  58. ## Deny access to any host other than (www.)mydomain.com  

  59. server {  

  60. server_name _; #default  

  61. return 444;  

  62. }  

  63. ## Server (www.)mydomain.com  

  64. server {  

  65. access_log /var/log/Nginx/access.log main buffer=32k;  

  66. error_log /var/log/Nginx/error.log info;  

  67. expires 31d;  

  68. limit_conn gulag 5;  

  69. listen 127.0.0.1:8080 rcvbuf=64k backlog=128;  

  70. root /disk01/htdocs;  

  71. server_name mydomain.com www.mydomain;  

  72. ## SSL Options (only enable if you use a SSL certificate)  

  73. # ssl on;  

  74. # ssl_certificate /ssl_keys/mydomain.com_ssl.crt;  

  75. # ssl_certificate_key /ssl_keys/mydomain_ssl.key;  

  76. # ssl_ciphers HIGH:!ADH:!MD5;  

  77. # ssl_prefer_server_ciphers on;  

  78. # ssl_protocols SSLv3;  

  79. # ssl_session_cache shared:SSL:1m;  

  80. # ssl_session_timeout 5m;  

  81. ## Only allow GET and HEAD request methods  

  82. if ($request_method !~ ^(GET|HEAD)$ ) {  

  83. return 444;  

  84. }  

  85. ## Deny illegal Host headers  

  86. if ($host !~* ^(mydomain.com|www.mydomain.com)$ ) {  

  87. return 444;  

  88. }  

  89. ## Deny certain User-Agents (case insensitive)  

  90. ## The ~* makes it case insensitive as opposed to just a ~  

  91. if ($http_user_agent ~* (Baiduspider|Jullo) ) {  

  92. return 444;  

  93. }  

  94. ## Deny certain Referers (case insensitive)  

  95. ## The ~* makes it case insensitive as opposed to just a ~  

  96. if ($http_referer ~* (babes|click|diamond|forsale|girl|jewelry|
    love|nudit|organic|poker|porn|poweroversoftware|sex|teen|
    video|webcam|zippo) ) {  

  97. return 444;  

  98. }  

  99. ## Redirect from www to non-www  

  100. if ($host = 'www.mydomain.com' ) {  

  101. rewrite ^/(.*)$ http://mydomain.com/$1 permanent;  

  102. }  

  103. ## Stop Image and Document Hijacking  

  104. location ~* (\.jpg|\.png|\.css)$ {  

  105. if ($http_referer !~ ^(http://mydomain.com) ) {  

  106. return 444;  

  107. }  

  108. }  

  109. ## Restricted Access directory  

  110. location ^~ /secure/ {  

  111. allow 127.0.0.1/32;  

  112. allow 10.10.10.0/24;  

  113. deny all;  

  114. auth_basic "RESTRICTED ACCESS";  

  115. auth_basic_user_file /var/www/htdocs/secure/access_list;  

  116. }  

  117. ## Only allow these file types to document root  

  118. location / {  

  119. if ($request_uri ~* (^\/|\.html|\.jpg|\.org|\.png|\.css|
    favicon\.ico|robots\.txt)$ ) {  

  120. break;  

  121. }  

  122. return 444;  

  123. }  

  124. ## Serve an empty 1x1 gif _OR_ an error 204 (No Content) 
    for favicon.ico  

  125. location = /favicon.ico {  

  126. #empty_gif;  

  127. return 204;  

  128. }  

  129. ## System Maintenance (Service Unavailable)   

  130. if (-f $document_root/system_maintenance.html ) {  

  131. error_page 503 /system_maintenance.html;  

  132. return 503;  

  133. }  

  134. ## All other errors get the generic error page  

  135. error_page 400 401 402 403 404 405 406 407 408 409 410 411 
    412 413 414 415 416 417  

  136. 500 501 502 503 504 505 /error_page.html;  

  137. location /error_page.html {  

  138. internal;  

  139. }  

  140. }  

  141. }  

  142. #  

  143. #######################################################  

  144. ### Calomel.org /etc/Nginx.conf END  

  145. #######################################################  

上述内容就是Nginx中怎么配置静态文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI