nginx域名反向代理配置(https/负载均衡):nginx.conf

àì夳堔傛蜴生んèń 2022-01-28 04:11 415阅读 0赞

简介

  1. nginx域名反向代理,整合http和https,同时实现iphash的负载均衡配置;
  2. 本篇博客展示nginx的配置文件nginx.conf的详细配置;

实践

  1. #user nobody;
  2. worker_processes 1; #lscpu修改成内核数
  3. events {
  4. worker_connections 1024; #修改成:65535
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=licache:10m inactive=5m;
  10. fastcgi_cache_key "$request_method://$host$request_uri";
  11. fastcgi_connect_timeout 300;
  12. fastcgi_send_timeout 300;
  13. fastcgi_read_timeout 300;
  14. fastcgi_buffer_size 64k;
  15. fastcgi_buffers 8 64k;
  16. fastcgi_busy_buffers_size 128k;
  17. fastcgi_temp_file_write_size 128k;
  18. fastcgi_cache licache;
  19. fastcgi_cache_valid 200 302 1h;
  20. fastcgi_cache_valid 301 1d;
  21. fastcgi_cache_valid any 1m;
  22. fastcgi_cache_min_uses 1;
  23. fastcgi_cache_use_stale error timeout invalid_header http_500;
  24. open_file_cache max=65535 inactive=20s;
  25. open_file_cache_min_uses 1;
  26. open_file_cache_valid 30s;
  27. sendfile on;
  28. keepalive_timeout 120;
  29. upstream tomcat1{ #配置代理映射ip以及端口
  30. server 192.168.2.22:8080;
  31. }
  32. upstream tomcat2{
  33. ip_hash; #配置iphash,多台服务器负载,不需要则去掉
  34. server 192.168.2.22:8081;
  35. server 192.168.2.23:8081;
  36. }
  37. #1. nginx默认接入端口为80;
  38. #2. 多少个域名配置则配置使用多少个server;
  39. #3. nginx接入https端口默认是443;
  40. #配置nginx域名默认访问页面
  41. server{
  42. listen 80;
  43. server_name www.admin.com; #申请的域名
  44. location / {
  45. root webapp; #表示sbin同级目录webapp,也可设置根目录
  46. index index.html; #默认访问的html
  47. }
  48. }
  49. #配置http反向代理域名1
  50. server {
  51. listen 80;
  52. server_name www.admin.com; #申请的域名
  53. location / {
  54. proxy_pass http://tomcat1; #区分域名映射的tomcat
  55. index index.html index.htm;
  56. }
  57. error_page 500 502 503 504 /50x.html;
  58. location = /50x.html {
  59. root html;
  60. }
  61. }
  62. #配置http反向代理域名2
  63. server {
  64. listen 80;
  65. server_name www.test.com; #申请的域名
  66. location / {
  67. proxy_pass http://tomcat2; #区分域名映射的tomcat
  68. index index.html index.htm;
  69. }
  70. error_page 500 502 503 504 /50x.html;
  71. location = /50x.html {
  72. root html;
  73. }
  74. }
  75. # 配置https反向代理域名,默认监听端口443,需自行申请ssl证书,下载证书到同级目录下,引入即可
  76. server {
  77. listen 443;
  78. server_name open.36change.com;
  79. ssl on;
  80. root html;
  81. index index.html index.htm;
  82. ssl_certificate cert/admin/214877699140754.pem; #ssl证书
  83. ssl_certificate_key cert/admin/214877699140754.key; #ssl证书秘钥
  84. ssl_session_timeout 5m;
  85. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  86. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  87. ssl_prefer_server_ciphers on;
  88. location / {
  89. proxy_pass http://tomcat1; #https域名对应的tomcat
  90. index index.html index.htm;
  91. client_max_body_size 40M;
  92. }
  93. }
  94. server {
  95. listen 443;
  96. server_name www.opensporting.com;
  97. ssl on;
  98. root html;
  99. index index.html index.htm;
  100. ssl_certificate cert/test/214926212910754.pem; #ssl证书
  101. ssl_certificate_key cert/test/214926212910754.key; #ssl证书秘钥
  102. ssl_session_timeout 5m;
  103. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  104. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  105. ssl_prefer_server_ciphers on;
  106. location / {
  107. proxy_pass http://tomcat2; #https域名对应的tomcat
  108. index index.html index.htm;
  109. client_max_body_size 1000M;
  110. proxy_connect_timeout 600;
  111. proxy_send_timeout 600;
  112. proxy_read_timeout 600;
  113. proxy_buffer_size 32k;
  114. proxy_buffers 32 256k;
  115. proxy_busy_buffers_size 512k;
  116. proxy_temp_file_write_size 512k;
  117. }
  118. }
  119. }

总结

实践是检验认识真理性的唯一标准,自己动手,丰衣足食~~

发表评论

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

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

相关阅读

    相关 Nginx配置反向代理负载均衡

    今天给大家介绍一下如何利用Nginx进行反向代理,之所以介绍这个的原因是,因为开发的时候遇到一个很尴尬的场景。因为是springboot项目,所以每一个控制类的端口都不一样,但