Nginx配置反向代理

自学项目时,有时会需要使用域名直接访问对应的网址或者执行负载均衡操作,这个时候可以使用nginx反向代理。

1.可以使用SwitchHost配置Host文件,方便在不同的环境进行测试。配置文件如下。
.当访问gulimall.com时,SwitchHost会在当前环境下的配置方案转发请求到192.168.56.10ip地址。

2.Nginx配置文件解析图

########nginx.conf#############
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    #负载均衡模块,会接收到上游传过来的网址信息转发到对应的网关服务或者直接访问服务
    upstream gulimall{
        server 192.168.119.1:88;
    }

    include /etc/nginx/conf.d/*.conf;
}

在反向代理中主要关心的是htttp模块,并且在最下方的include中指定了在那个地方读取配置文件。

3.在Nginx的conf.d文件夹中创建gulimall.conf文件,进行关于gullmall.com相关配置。

###########gulimall.conf##########
server {
	#监听80端口是否访问对应的网址,监听到了会代理给对应网关。
    listen       80;
    server_name  gulimall.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
    	#设置header是否携带信息,默认在转发http请求时会丢失host信息。
        proxy_set_header Host $host;
        #Nginx反向代理到gulimall,上面的nginx.conf的upstream模块可以继续配置。
        proxy_pass http://gulimall;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
##application.yml文件配置,类似**.gulimall.com适配性广的表达式配置最好放在最后以免影响其他路由转发
spring:
  cloud:
    gateway:
      routes:
        - id: gulimall_host_route
          uri: lb://gulimall-product
          predicates:
            - Host=**.gulimall.com

predicates的Host会解析http请求头中的Host信息,并且转发到uri指定的服务中。

总结
在配置反向代理中主要考虑Nginx文件中的nginx.conf文件和在conf.d文件夹中自定义的配置文件。
1.当Nginx监测到端口中有发送网址信息时,会根据conf.d文件夹中自定义的配置文件转发,此时这是上游服务。
2.配置的nginx.conf在配置upstream模块接收到请求后转发到对应的ip地址。从而完成反向代理。
在阿里云上配置的代理不适用于本教程,可以考虑购买网址和穿透技术在云服务器进行反向代理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值