Nginx反向代理+负载均衡简单实现(http方式)(2)

-----------------------------------------------------------------------------------------------------------------
下面这个匹配path的代理设置需要注意几点:
首先一定要保证目标B机器,也就是192.168.1.102的8090端口站点目录下有这个匹配path的目录ios存在!!
也就是要保证A机器本机能顺利访问到目标B机器的8090端口的ios路径,即:
[root@nginx-1.8.0 vhosts]# curl :8090/ios/ #一定要保证这个能从A机器访问成功!

下面几种配置都是可以的:

第一种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {

    listen 8090;

    server_name localhost;

    index index.html index.php index.htm;

    root /var/www/html;

  

    access_log  /usr/local/nginx/logs/8090-access.log main;

    error_log  /usr/local/nginx/logs/8090-error.log;

 

    location /ios/ {                            #这种情况,这里一定要匹配的是/ios/,不能是/ios

    proxy_pass http://192.168.1.102:8090;      #一定要保证192.168.1.102机器8090端口站点目录下有ios目录!否则访问会报错404!

    proxy_redirect off ;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header REMOTE-HOST $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 300;           

    proxy_send_timeout 300;             

    proxy_read_timeout 600;             

    proxy_buffer_size 256k;               

    proxy_buffers 4 256k;                 

    proxy_busy_buffers_size 256k;       

    proxy_temp_file_write_size 256k;     

    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

    proxy_max_temp_file_size 128m;

}

}

第二种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {

    listen 8090;

    server_name localhost;

    index index.html index.php index.htm;

    root /var/www/html;

  

    access_log  /usr/local/nginx/logs/8090-access.log main;

    error_log  /usr/local/nginx/logs/8090-error.log;

 

    location /ios/ {

    proxy_pass http://192.168.1.102:8090/ios/;

    proxy_redirect off ;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header REMOTE-HOST $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 300;           

    proxy_send_timeout 300;             

    proxy_read_timeout 600;             

    proxy_buffer_size 256k;               

    proxy_buffers 4 256k;                 

    proxy_busy_buffers_size 256k;       

    proxy_temp_file_write_size 256k;     

    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

    proxy_max_temp_file_size 128m;

}

}

第三种:
[root@nginx-1.8.0 vhosts]# cat 8090.conf

server {

    listen 8090;

    server_name localhost;

    index index.html index.php index.htm;

    root /var/www/html;

  

    access_log  /usr/local/nginx/logs/8090-access.log main;

    error_log  /usr/local/nginx/logs/8090-error.log;

 

    location /ios {

    proxy_pass http://192.168.1.102:8090/ios/;        这种情况,这里一定要匹配的是/ios/,不能是/ios

    proxy_redirect off ;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header REMOTE-HOST $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 300;           

    proxy_send_timeout 300;             

    proxy_read_timeout 600;             

    proxy_buffer_size 256k;               

    proxy_buffers 4 256k;                 

    proxy_busy_buffers_size 256k;       

    proxy_temp_file_write_size 256k;     

    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

    proxy_max_temp_file_size 128m;

}

}

以上三种配置方法都保证了访问:8090/ios会自动变为:8090/ios/,并代理到:8090/ios/的结果

-----------------------------------------------------------------------------------------------------------------

[root@nginx-1.8.0 vhosts]# cat LB.conf

upstream lb {

    server 192.168.1.102:80 max_fails=3 fail_timeout=30s;  #max_fails = 3 为允许失败的次数,默认值为1

    server 192.168.1.103:80 max_fails=3 fail_timeout=30s;  #fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间

}

 

server {

    listen 80;

    server_name localhost;

    index index.html index.php index.htm;

    root /var/www/html;

  

    access_log  /usr/local/nginx/logs/80-access.log main;

    error_log  /usr/local/nginx/logs/80-error.log;

 

    location / {

    proxy_pass http://lb;

    proxy_redirect off ;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header REMOTE-HOST $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 300;           

    proxy_send_timeout 300;             

    proxy_read_timeout 600;             

    proxy_buffer_size 256k;               

    proxy_buffers 4 256k;                 

    proxy_busy_buffers_size 256k;       

    proxy_temp_file_write_size 256k;     

    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

    proxy_max_temp_file_size 128m;

}

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/0a4f128b0e00dfe1bf234a6f7b7e3dcc.html