[乐意黎]Nginx 参数配置示例

爱被打了一巴掌 2022-04-04 20:10 442阅读 0赞

nginx.conf

  1. user www www; ## Default: nobody
  2. worker_processes 5; ## Default: 1
  3. error_log logs/error.log;
  4. pid logs/nginx.pid;
  5. worker_rlimit_nofile 8192;
  6. events {
  7. worker_connections 4096; ## Default: 1024
  8. }
  9. http {
  10. include conf/mime.types;
  11. include /etc/nginx/proxy.conf;
  12. include /etc/nginx/fastcgi.conf;
  13. index index.html index.htm index.php;
  14. default_type application/octet-stream;
  15. log_format main '$remote_addr - $remote_user [$time_local] $status '
  16. '"$request" $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. access_log logs/access.log main;
  19. sendfile on;
  20. tcp_nopush on;
  21. server_names_hash_bucket_size 128; # this seems to be required for some vhosts
  22. server {
  23. # php/fastcgi
  24. listen 80;
  25. server_name domain1.com www.domain1.com;
  26. access_log logs/domain1.access.log main;
  27. root html;
  28. location ~ \.php$ {
  29. #php-fpm 端口定义见 /usr/local/php/etc/php-fpm.d/www.conf
  30. fastcgi_pass 127.0.0.1:9000;
  31. }
  32. }
  33. server {
  34. # simple reverse-proxy
  35. listen 80;
  36. server_name domain2.com www.domain2.com;
  37. access_log logs/domain2.access.log main;
  38. # serve static files
  39. location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  40. root /var/www/virtual/big.server.com/htdocs;
  41. expires 30d;
  42. }
  43. # pass requests for dynamic content to rails/turbogears/zope, et al
  44. location / {
  45. #php-fpm 端口定义见 /usr/local/php/etc/php-fpm.d/www.conf
  46. proxy_pass http://127.0.0.1:8080;
  47. }
  48. }
  49. upstream big_server_com {
  50. server 127.0.0.3:8000 weight=5;
  51. server 127.0.0.3:8001 weight=5;
  52. server 192.168.0.1:8000;
  53. server 192.168.0.1:8001;
  54. }
  55. server { # simple load balancing
  56. listen 80;
  57. server_name big.server.com;
  58. access_log logs/big.server.access.log main;
  59. location / {
  60. proxy_pass http://big_server_com;
  61. }
  62. }
  63. }

proxy.conf¶

  1. proxy_redirect off;
  2. proxy_set_header Host $host;
  3. proxy_set_header X-Real-IP $remote_addr;
  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  5. client_max_body_size 10m;
  6. client_body_buffer_size 128k;
  7. proxy_connect_timeout 90;
  8. proxy_send_timeout 90;
  9. proxy_read_timeout 90;
  10. proxy_buffers 32 4k;

fastcgi.conf

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  2. fastcgi_param QUERY_STRING $query_string;
  3. fastcgi_param REQUEST_METHOD $request_method;
  4. fastcgi_param CONTENT_TYPE $content_type;
  5. fastcgi_param CONTENT_LENGTH $content_length;
  6. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  7. fastcgi_param REQUEST_URI $request_uri;
  8. fastcgi_param DOCUMENT_URI $document_uri;
  9. fastcgi_param DOCUMENT_ROOT $document_root;
  10. fastcgi_param SERVER_PROTOCOL $server_protocol;
  11. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  12. fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
  13. fastcgi_param REMOTE_ADDR $remote_addr;
  14. fastcgi_param REMOTE_PORT $remote_port;
  15. fastcgi_param SERVER_ADDR $server_addr;
  16. fastcgi_param SERVER_PORT $server_port;
  17. fastcgi_param SERVER_NAME $server_name;
  18. fastcgi_index index.php;
  19. fastcgi_param REDIRECT_STATUS 200;

mime.types

  1. types {
  2. text/html html htm shtml;
  3. text/css css;
  4. text/xml xml rss;
  5. image/gif gif;
  6. image/jpeg jpeg jpg;
  7. application/x-javascript js;
  8. text/plain txt;
  9. text/x-component htc;
  10. text/mathml mml;
  11. image/png png;
  12. image/x-icon ico;
  13. image/x-jng jng;
  14. image/vnd.wap.wbmp wbmp;
  15. application/java-archive jar war ear;
  16. application/mac-binhex40 hqx;
  17. application/pdf pdf;
  18. application/x-cocoa cco;
  19. application/x-java-archive-diff jardiff;
  20. application/x-java-jnlp-file jnlp;
  21. application/x-makeself run;
  22. application/x-perl pl pm;
  23. application/x-pilot prc pdb;
  24. application/x-rar-compressed rar;
  25. application/x-redhat-package-manager rpm;
  26. application/x-sea sea;
  27. application/x-shockwave-flash swf;
  28. application/x-stuffit sit;
  29. application/x-tcl tcl tk;
  30. application/x-x509-ca-cert der pem crt;
  31. application/x-xpinstall xpi;
  32. application/zip zip;
  33. application/octet-stream deb;
  34. application/octet-stream bin exe dll;
  35. application/octet-stream dmg;
  36. application/octet-stream eot;
  37. application/octet-stream iso img;
  38. application/octet-stream msi msp msm;
  39. audio/mpeg mp3;
  40. audio/x-realaudio ra;
  41. video/mpeg mpeg mpg;
  42. video/quicktime mov;
  43. video/x-flv flv;
  44. video/x-msvideo avi;
  45. video/x-ms-wmv wmv;
  46. video/x-ms-asf asx asf;
  47. video/x-mng mng;
  48. }

nginx.conf¶

  1. user www www;
  2. worker_processes 2;
  3. pid /var/run/nginx.pid;
  4. # [ debug | info | notice | warn | error | crit ]
  5. error_log /var/log/nginx.error_log info;
  6. events {
  7. worker_connections 2000;
  8. # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
  9. use kqueue;
  10. }
  11. http {
  12. include conf/mime.types;
  13. default_type application/octet-stream;
  14. log_format main '$remote_addr - $remote_user [$time_local] '
  15. '"$request" $status $bytes_sent '
  16. '"$http_referer" "$http_user_agent" '
  17. '"$gzip_ratio"';
  18. log_format download '$remote_addr - $remote_user [$time_local] '
  19. '"$request" $status $bytes_sent '
  20. '"$http_referer" "$http_user_agent" '
  21. '"$http_range" "$sent_http_content_range"';
  22. client_header_timeout 3m;
  23. client_body_timeout 3m;
  24. send_timeout 3m;
  25. client_header_buffer_size 1k;
  26. large_client_header_buffers 4 4k;
  27. gzip on;
  28. gzip_min_length 1100;
  29. gzip_buffers 4 8k;
  30. gzip_types text/plain;
  31. output_buffers 1 32k;
  32. postpone_output 1460;
  33. sendfile on;
  34. tcp_nopush on;
  35. tcp_nodelay on;
  36. send_lowat 12000;
  37. keepalive_timeout 75 20;
  38. # lingering_time 30;
  39. # lingering_timeout 10;
  40. # reset_timedout_connection on;
  41. server {
  42. listen one.example.com;
  43. server_name one.example.com www.one.example.com;
  44. access_log /var/log/nginx.access_log main;
  45. location / {
  46. proxy_pass http://127.0.0.1/;
  47. proxy_redirect off;
  48. proxy_set_header Host $host;
  49. proxy_set_header X-Real-IP $remote_addr;
  50. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  51. client_max_body_size 10m;
  52. client_body_buffer_size 128k;
  53. client_body_temp_path /var/nginx/client_body_temp;
  54. proxy_connect_timeout 90;
  55. proxy_send_timeout 90;
  56. proxy_read_timeout 90;
  57. proxy_send_lowat 12000;
  58. proxy_buffer_size 4k;
  59. proxy_buffers 4 32k;
  60. proxy_busy_buffers_size 64k;
  61. proxy_temp_file_write_size 64k;
  62. proxy_temp_path /var/nginx/proxy_temp;
  63. charset koi8-r;
  64. }
  65. error_page 404 /404.html;
  66. location /404.html {
  67. root /spool/www;
  68. charset on;
  69. source_charset koi8-r;
  70. }
  71. location /old_stuff/ {
  72. rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;
  73. }
  74. location /download/ {
  75. valid_referers none blocked server_names *.example.com;
  76. if ($invalid_referer) {
  77. #rewrite ^/ http://www.example.com/;
  78. return 403;
  79. }
  80. # rewrite_log on;
  81. # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
  82. rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;
  83. root /spool/www;
  84. # autoindex on;
  85. access_log /var/log/nginx-download.access_log download;
  86. }
  87. location ~* ^.+\.(jpg|jpeg|gif)$ {
  88. root /spool/www;
  89. access_log off;
  90. expires 30d;
  91. }
  92. }
  93. }

参考: https://www.nginx.com/resources/wiki/start/topics/examples/full/

发表评论

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

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

相关阅读