当前位置:Gxlcms > PHP教程 > 一个nignx服务反向代理3个IP访问的配置方法

一个nignx服务反向代理3个IP访问的配置方法

时间:2021-07-01 10:21:17 帮助过:6人阅读

场景:在10.17.55,44:7002和10.17.55.46:7003的weblogic 部署一个web工程,准备做负载均衡。在一台IP:12.17.56.44能访问到55.44和55.46上web工程的机器做nginx代理服务器,代理端口为7000,既12.17.56.44:7000能访问到10.17.55,44:7002或10.17.55.46:7003的web工程,而互联网有个IP22.18.55.33:7000 可以访问到,12.17.56.44:7000,12.17.56.44这台服务器的nginx.conf 内容配置如下:

  1. #user nobody;
  2. worker_processes auto;
  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 2048;
  9. accept_mutex off;
  10. multi_accept on;
  11. use epoll;
  12. }
  13. # load modules compiled as Dynamic Shared Object (DSO)
  14. #
  15. #dso {
  16. # load ngx_http_fastcgi_module.so;
  17. # load ngx_http_rewrite_module.so;
  18. #}
  19. #thread_pool one threads=128 max_queue=0;
  20. http {
  21. include mime.types;
  22. default_type application/octet-stream;
  23. #thread_pool one threads=128 max_queue=0;
  24. include /etc/nginx/mutnginx/*.conf;
  25. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  26. # '$status $body_bytes_sent "$http_referer" '
  27. # '"$http_user_agent" "$http_x_forwarded_for"';
  28. #access_log logs/access.log main;
  29. access_log off;
  30. sendfile on;
  31. #tcp_nopush on;
  32. sendfile_max_chunk 512k;
  33. #keepalive_timeout 0;
  34. keepalive_timeout 65;
  35. upstream cluster1 {
  36. # simple round-robin
  37. ip_hash;
  38. server 10.17.55,44:7002;
  39. server 10.17.55.46:7003;
  40. }
  41. #gzip on;
  42. server {
  43. listen 7000;
  44. server_name localhost;
  45. # 0.99.132.52:700
  46. #check interval=30
  47. #charset koi8-r;
  48. #access_log logs/host.access.log main;
  49. location / {
  50. #aio threads=one;
  51. proxy_set_header Host $host:7000;
  52. proxy_set_header X-Real-IP $remote_addr;
  53. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  54. proxy_buffering off;
  55. proxy_pass http://cluster1/;
  56. # root html;
  57. #index index.html index.htm;
  58. }
  59. location /status {
  60. #check_status;
  61. access_log off;
  62. allow all;
  63. # deny all;
  64. }
  65. #error_page 404 /404.html;
  66. # redirect server error pages to the static page /50x.html
  67. #
  68. error_page 500 502 503 504 /50x.html;
  69. location = /50x.html {
  70. root html;
  71. }
  72. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  73. #
  74. #location ~ \.php$ {
  75. # proxy_pass http://127.0.0.1;
  76. #}
  77. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  78. #
  79. #location ~ \.php$ {
  80. # root html;
  81. # fastcgi_pass 127.0.0.1:9000;
  82. # fastcgi_index index.php;
  83. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  84. # include fastcgi_params;
  85. #}
  86. # deny access to .htaccess files, if Apache's document root
  87. # concurs with nginx's one
  88. #
  89. #location ~ /\.ht {
  90. # deny all;
  91. #}
  92. }
  93. # another virtual host using mix of IP-, name-, and port-based configuration
  94. #
  95. #server {
  96. # listen 8000;
  97. # listen somename:8080;
  98. # server_name somename alias another.alias;
  99. # location / {
  100. # root html;
  101. # index index.html index.htm;
  102. # }
  103. #}
  104. # HTTPS server
  105. #
  106. #server {
  107. # listen 443;
  108. # server_name localhost;
  109. # ssl on;
  110. # ssl_certificate cert.pem;
  111. # ssl_certificate_key cert.key;
  112. # ssl_session_timeout 5m;
  113. # ssl_protocols SSLv2 SSLv3 TLSv1;
  114. # ssl_ciphers HIGH:!aNULL:!MD5;
  115. # ssl_prefer_server_ciphers on;
  116. # location / {
  117. # root html;
  118. # index index.html index.htm;
  119. # }
  120. #}
  121. }

配置文件中:

  1. include /etc/nginx/mutnginx/*.conf;
是在mutnginx目录下,创建了两个跟nginx.conf 相似的文件。

nginx2.conf配置是为了让同个网段即12.17.56.*的其他应用能同个代理ip访问到web工程,其配置如下:

  1. #user nobody;
  2. worker_processes auto;
  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 2048;
  9. accept_mutex off;
  10. multi_accept on;
  11. use epoll;
  12. }
  13. # load modules compiled as Dynamic Shared Object (DSO)
  14. #
  15. #dso {
  16. # load ngx_http_fastcgi_module.so;
  17. # load ngx_http_rewrite_module.so;
  18. #}
  19. #thread_pool one threads=128 max_queue=0;
  20. http {
  21. include mime.types;
  22. default_type application/octet-stream;
  23. #thread_pool one threads=128 max_queue=0;
  24. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  25. # '$status $body_bytes_sent "$http_referer" '
  26. # '"$http_user_agent" "$http_x_forwarded_for"';
  27. #access_log logs/access.log main;
  28. access_log off;
  29. sendfile on;
  30. #tcp_nopush on;
  31. sendfile_max_chunk 512k;
  32. #keepalive_timeout 0;
  33. keepalive_timeout 65;
  34. upstream cluster3 {
  35. # simple round-robin
  36. ip_hash;
  37. server 10.17.55,44:7002;
  38. server 10.17.55.46:7003;
  39. }
  40. #gzip on;
  41. server {
  42. listen 7000;
  43. server_name localhost;
  44. # 0.99.132.52:700
  45. #check interval=30
  46. #charset koi8-r;
  47. #access_log logs/host.access.log main;
  48. location / {
  49. #aio threads=one;
  50. proxy_set_header Host $host:7000;
  51. proxy_set_header X-Real-IP $remote_addr;
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. proxy_buffering off;
  54. proxy_pass http://cluster3/;
  55. # root html;
  56. #index index.html index.htm;
  57. }
  58. location /status {
  59. #check_status;
  60. access_log off;
  61. allow all;
  62. # deny all;
  63. }
  64. #error_page 404 /404.html;
  65. # redirect server error pages to the static page /50x.html
  66. #
  67. error_page 500 502 503 504 /50x.html;
  68. location = /50x.html {
  69. root html;
  70. }
  71. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  72. #
  73. #location ~ \.php$ {
  74. # proxy_pass http://127.0.0.1;
  75. #}
  76. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  77. #
  78. #location ~ \.php$ {
  79. # root html;
  80. # fastcgi_pass 127.0.0.1:9000;
  81. # fastcgi_index index.php;
  82. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  83. # include fastcgi_params;
  84. #}
  85. # deny access to .htaccess files, if Apache's document root
  86. # concurs with nginx's one
  87. #
  88. #location ~ /\.ht {
  89. # deny all;
  90. #}
  91. }
  92. # another virtual host using mix of IP-, name-, and port-based configuration
  93. #
  94. #server {
  95. # listen 8000;
  96. # listen somename:8080;
  97. # server_name somename alias another.alias;
  98. # location / {
  99. # root html;
  100. # index index.html index.htm;
  101. # }
  102. #}
  103. # HTTPS server
  104. #
  105. #server {
  106. # listen 443;
  107. # server_name localhost;
  108. # ssl on;
  109. # ssl_certificate cert.pem;
  110. # ssl_certificate_key cert.key;
  111. # ssl_session_timeout 5m;
  112. # ssl_protocols SSLv2 SSLv3 TLSv1;
  113. # ssl_ciphers HIGH:!aNULL:!MD5;
  114. # ssl_prefer_server_ciphers on;
  115. # location / {
  116. # root html;
  117. # index index.html index.htm;
  118. # }
  119. #}
  120. }

还有一个nginx3.conf 是为了让,其他内网ip统一访问的代理,其配置如下:

  1. #user nobody;
  2. worker_processes auto;
  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 2048;
  9. accept_mutex off;
  10. multi_accept on;
  11. use epoll;
  12. }
  13. # load modules compiled as Dynamic Shared Object (DSO)
  14. #
  15. #dso {
  16. # load ngx_http_fastcgi_module.so;
  17. # load ngx_http_rewrite_module.so;
  18. #}
  19. #thread_pool one threads=128 max_queue=0;
  20. http {
  21. include mime.types;
  22. default_type application/octet-stream;
  23. #thread_pool one threads=128 max_queue=0;
  24. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  25. # '$status $body_bytes_sent "$http_referer" '
  26. # '"$http_user_agent" "$http_x_forwarded_for"';
  27. #access_log logs/access.log main;
  28. access_log off;
  29. sendfile on;
  30. #tcp_nopush on;
  31. sendfile_max_chunk 512k;
  32. #keepalive_timeout 0;
  33. keepalive_timeout 65;
  34. upstream cluster2 {
  35. # simple round-robin
  36. ip_hash;
  37. server 10.17.55,44:7002;
  38. server 10.17.55.46:7003;
  39. }
  40. #gzip on;
  41. server {
  42. listen 7000;
  43. server_name localhost;
  44. # 0.99.132.52:700
  45. #check interval=30
  46. #charset koi8-r;
  47. #access_log logs/host.access.log main;
  48. location / {
  49. #aio threads=one;
  50. proxy_set_header Host $host:5123;
  51. proxy_set_header X-Real-IP $remote_addr;
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. proxy_buffering off;
  54. proxy_pass http://cluster2/;
  55. # root html;
  56. #index index.html index.htm;
  57. }
  58. location /status {
  59. #check_status;
  60. access_log off;
  61. allow all;
  62. # deny all;
  63. }
  64. #error_page 404 /404.html;
  65. # redirect server error pages to the static page /50x.html
  66. #
  67. error_page 500 502 503 504 /50x.html;
  68. location = /50x.html {
  69. root html;
  70. }
  71. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  72. #
  73. #location ~ \.php$ {
  74. # proxy_pass http://127.0.0.1;
  75. #}
  76. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  77. #
  78. #location ~ \.php$ {
  79. # root html;
  80. # fastcgi_pass 127.0.0.1:9000;
  81. # fastcgi_index index.php;
  82. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  83. # include fastcgi_params;
  84. #}
  85. # deny access to .htaccess files, if Apache's document root
  86. # concurs with nginx's one
  87. #
  88. #location ~ /\.ht {
  89. # deny all;
  90. #}
  91. }
  92. # another virtual host using mix of IP-, name-, and port-based configuration
  93. #
  94. #server {
  95. # listen 8000;
  96. # listen somename:8080;
  97. # server_name somename alias another.alias;
  98. # location / {
  99. # root html;
  100. # index index.html index.htm;
  101. # }
  102. #}
  103. # HTTPS server
  104. #
  105. #server {
  106. # listen 443;
  107. # server_name localhost;
  108. # ssl on;
  109. # ssl_certificate cert.pem;
  110. # ssl_certificate_key cert.key;
  111. # ssl_session_timeout 5m;
  112. # ssl_protocols SSLv2 SSLv3 TLSv1;
  113. # ssl_ciphers HIGH:!aNULL:!MD5;
  114. # ssl_prefer_server_ciphers on;
  115. # location / {
  116. # root html;
  117. # index index.html index.htm;
  118. # }
  119. #}
  120. }
其中
  1. proxy_set_header Host $host:5123;#是映射端口,如果12.17.56.44:7000映射IP为102.34.56.44 那么5123就是映射器7000端口
这样
  1. 12.17.56.44
就被一个互联网IP22.18.55.33:7000 和内网12.17.56.44.:5213以及12.17.56.44:7000做了映射代理,就是这三个ip都能访问到web工程。

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了一个nignx 服务反向代理3个IP访问的配置方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行