nginx(四) nginx用法实例

我不是女神ヾ 2022-12-01 01:22 428阅读 0赞

本人常用的机器是windows,下面在windows环境下演示下Nginx的用法

一、代理后端接口:如我本地在9999端口启动一个服务:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70

现在使用nginx代理到这个端口:此时访问localhost:9991会代理localhost:9999

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 1

通过代理访问接口:localhost:9999/demo/code/getCode使用localhost:9999 = localhost:9991规则来替换,得到localhost:9991/demo/code/getCode

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 2

如果Nginx换个写法:

  1. server {
  2. listen 9991;
  3. server_name localhost;
  4. location / {
  5. root html;
  6. index index.html index.htm;
  7. proxy_pass http://localhost:9999/demo/;
  8. }

这时候访问localhost:9991会代理到localhost:9999/demo/,原地址localhost:9999/demo/code/getCod替换下,所以访问代理的url为:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 3

二、代理前端页面:

20200826151732461.png

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. proxy_pass http://localhost:9999;
  10. }
  11. #访问方式:http://localhost/test/或http://localhost/test
  12. location ^~ /test{
  13. alias F:/aaa;
  14. index test.html;
  15. autoindex on;
  16. autoindex_exact_size off;
  17. autoindex_localtime on;
  18. ssi on;
  19. ssi_silent_errors on;
  20. proxy_redirect off;
  21. proxy_set_header Host $host;
  22. proxy_set_header X-Real-IP $remote_addr;
  23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. proxy_set_header X-Forwarded-Proto $scheme;
  25. }

20200826161625451.png

三、代理静态资源:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. proxy_pass http://localhost:9999;
  10. }
  11. #访问方式:http://localhost/file/或http://localhost/file
  12. location ^~ /file{
  13. alias E:/jdk1.8;
  14. autoindex on;
  15. autoindex_exact_size off;
  16. autoindex_localtime on;
  17. ssi on;
  18. ssi_silent_errors on;
  19. proxy_redirect off;
  20. proxy_set_header Host $host;
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23. proxy_set_header X-Forwarded-Proto $scheme;
  24. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 4

四、使用同一个Nginx配置文件同时代理多个后端服务、静态资源:

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include mime.types;
  6. default_type application/octet-stream;
  7. sendfile on;
  8. keepalive_timeout 65;
  9. server {
  10. listen 80;
  11. server_name localhost;
  12. #访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
  13. location / {
  14. root html;
  15. index index.html index.htm;
  16. proxy_pass http://localhost:9999;
  17. }
  18. #访问方式:http://localhost/mytest/demo/code/getCode,代理到http://localhost:7777/demo/code/getCode接口
  19. location /mytest/ {
  20. proxy_pass http://localhost:7777/;
  21. }
  22. #访问方式:http://localhost/test/或http://localhost/test
  23. location ^~ /test{
  24. alias F:/aaa;
  25. index test.html;
  26. autoindex on;
  27. autoindex_exact_size off;
  28. autoindex_localtime on;
  29. ssi on;
  30. ssi_silent_errors on;
  31. proxy_redirect off;
  32. proxy_set_header Host $host;
  33. proxy_set_header X-Real-IP $remote_addr;
  34. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  35. proxy_set_header X-Forwarded-Proto $scheme;
  36. }
  37. #访问方式:http://localhost/file/或http://localhost/file
  38. location ^~ /file{
  39. alias E:;
  40. autoindex on;
  41. autoindex_exact_size off;
  42. autoindex_localtime on;
  43. ssi on;
  44. ssi_silent_errors on;
  45. proxy_redirect off;
  46. proxy_set_header Host $host;
  47. proxy_set_header X-Real-IP $remote_addr;
  48. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  49. proxy_set_header X-Forwarded-Proto $scheme;
  50. }
  51. }
  52. }

五、使用Nginx实现负载均衡:使用upstream。

如同一个服务在本地分别启动9999和7777两个端口:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 5 watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 6

修改nginx配置并重启:注意weight和=中间不要有空格

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfdF95X3k_size_16_color_FFFFFF_t_70 7

  1. 这时候访问localhost:9991/demo/code/getCode,可以看到本地99997777两个端口在轮询调用。

再如:

  1. upstream mydemoservice{
  2. server localhost:7777 weight=5;
  3. server localhost:9999 weight=5;
  4. }
  5. server {
  6. listen 80;
  7. server_name localhost;
  8. #访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
  9. location / {
  10. root html;
  11. index index.html index.htm;
  12. proxy_pass http://localhost:9999;
  13. }
  14. location /myserver/ {
  15. proxy_pass http://mydemoservice/;
  16. }

访问localhost/myserver/demo/code/getCode。

发表评论

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

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

相关阅读