1、选择nginx处理静态资源的原因

虽然Tomcat和nginx都可以处理静态资源,但是nginx效率较高,但这不是主要原因

2、流程

2.1、将动态资源放入两台Tomcat,可以使用catalina.sh run启动,打印日志到Linux控制台(略)

2.2、一台主nginx

2.2.1、配置文件

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. #配置两台Tomcat服务器的ip和端口号
  23. #dynamic resources ip & port
  24. upstream www.web.com {
  25. server 192.168.90.129:8081 weight=2;
  26. server 192.168.90.129:8082 weight=1;
  27. }
  28. #配置两台nginx服务器的ip和端口号
  29. #static resouces ip & port
  30. upstream static.resources {
  31. server 192.168.90.129:81;
  32. server 192.168.90.129:82;
  33. }
  34. server {
  35. listen 80;
  36. server_name localhost;
  37. #charset koi8-r;
  38. #access_log logs/host.access.log main;
  39. #配置动态资源的拦截地址
  40. #dynamic resources
  41. location / {
  42. proxy_pass http://www.web.com;
  43. }
  44. #配置静态资源的拦截地址
  45. #static resources
  46. location ~.*/(img){
  47. proxy_pass http://static.resources;
  48. }
  49. #error_page 404 /404.html;
  50. # redirect server error pages to the static page /50x.html
  51. #
  52. error_page 500 502 503 504 /50x.html;
  53. location = /50x.html {
  54. root html;
  55. }
  56. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  57. #
  58. #location ~ \.php$ {
  59. # proxy_pass http://127.0.0.1;
  60. #}
  61. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  62. #
  63. #location ~ \.php$ {
  64. # root html;
  65. # fastcgi_pass 127.0.0.1:9000;
  66. # fastcgi_index index.php;
  67. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  68. # include fastcgi_params;
  69. #}
  70. # deny access to .htaccess files, if Apache's document root
  71. # concurs with nginx's one
  72. #
  73. #location ~ /\.ht {
  74. # deny all;
  75. #}
  76. }
  77. # another virtual host using mix of IP-, name-, and port-based configuration
  78. #
  79. #server {
  80. # listen 8000;
  81. # listen somename:8080;
  82. # server_name somename alias another.alias;
  83. # location / {
  84. # root html;
  85. # index index.html index.htm;
  86. # }
  87. #}
  88. # HTTPS server
  89. #
  90. #server {
  91. # listen 443 ssl;
  92. # server_name localhost;
  93. # ssl_certificate cert.pem;
  94. # ssl_certificate_key cert.key;
  95. # ssl_session_cache shared:SSL:1m;
  96. # ssl_session_timeout 5m;
  97. # ssl_ciphers HIGH:!aNULL:!MD5;
  98. # ssl_prefer_server_ciphers on;
  99. # location / {
  100. # root html;
  101. # index index.html index.htm;
  102. # }
  103. #}
  104. }

2.3、两台处理静态资源的nginx

2.3.1、配置文件

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. #---------------------------
  24. #第一台nginx配置81端口
  25. listen 81;
  26. #第二台nginx配置82端口
  27. #listen 82;
  28. #----------------------------
  29. server_name localhost;
  30. #charset koi8-r;
  31. #access_log logs/host.access.log main;
  32. #----------------------------
  33. #配置静态资源文件的具体目录
  34. location ~.*/(img){
  35. root /opt/my/static;
  36. }
  37. #----------------------------
  38. #error_page 404 /404.html;
  39. # redirect server error pages to the static page /50x.html
  40. #
  41. error_page 500 502 503 504 /50x.html;
  42. location = /50x.html {
  43. root html;
  44. }
  45. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  46. #
  47. #location ~ \.php$ {
  48. # proxy_pass http://127.0.0.1;
  49. #}
  50. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  51. #
  52. #location ~ \.php$ {
  53. # root html;
  54. # fastcgi_pass 127.0.0.1:9000;
  55. # fastcgi_index index.php;
  56. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  57. # include fastcgi_params;
  58. #}
  59. # deny access to .htaccess files, if Apache's document root
  60. # concurs with nginx's one
  61. #
  62. #location ~ /\.ht {
  63. # deny all;
  64. #}
  65. }
  66. # another virtual host using mix of IP-, name-, and port-based configuration
  67. #
  68. #server {
  69. # listen 8000;
  70. # listen somename:8080;
  71. # server_name somename alias another.alias;
  72. # location / {
  73. # root html;
  74. # index index.html index.htm;
  75. # }
  76. #}
  77. # HTTPS server
  78. #
  79. #server {
  80. # listen 443 ssl;
  81. # server_name localhost;
  82. # ssl_certificate cert.pem;
  83. # ssl_certificate_key cert.key;
  84. # ssl_session_cache shared:SSL:1m;
  85. # ssl_session_timeout 5m;
  86. # ssl_ciphers HIGH:!aNULL:!MD5;
  87. # ssl_prefer_server_ciphers on;
  88. # location / {
  89. # root html;
  90. # index index.html index.htm;
  91. # }
  92. #}
  93. }