✅P139_商城业务-nginx-搭建域名访问环境---反向代理配置
2024/2/13大约 2 分钟
正向代理与反向代理
正向代理:如科学上网,隐藏客户端信息

反向代理:屏蔽内网服务器信息,负载均衡访问

host文件配置域名
域名解析的规则:
- 先解析本机是否有相应的映射规则
- host文件配置内容:# 127.0.0.1 localhost,含义为访问localhost即访问127.0.0.1
- DNS域名解析获取相应的ip地址
host文件位置:C:\Windows\System32\drivers\etc
添加配置内容:
# 虚拟机ip
192.168.17.130 cfmall.com修改host文件时注意将:右键->属性->只读去掉,允许编辑
Nginx+Windows搭建域名访问环境
让nginx帮我们进行反向代理,所有来自原cfmall.com的请求,都转到商品服务

域名映射效果
- 请求接口 cfmall.com
- 请求页面 cfmall.com
- nginx直接代理给网关,网关判断 - 如果/api/****,转交给对应的服务器
- 如果是满足域名,转交给对应的服务
 
- 如果
Nginx配置文件
组成

内容详解
cat /mydata/nginx/conf/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;
    include /etc/nginx/conf.d/*.conf;
}include /etc/nginx/conf.d/*.conf;说明在 conf.d 目录下所有 .conf 后缀的文件内容都会作为 nginx 配置文件 http 块中的配置。这是为了防止主配置文件太复杂,也可以对不同的配置进行分类。
配置cfmall.conf
默认配置下,我们访问 cfmall.com 会请求 nginx 默认的 index 页面,现在我们要做的是当访问 cfmall.com 的时候转发到我们的商品模块的商城首页界面。
参考 /mydata/nginx/conf/conf.d 目录下default.conf配置,来配置 cfmall 的 server 块配置。
cp default.conf cfmall.conf

cfmall.conf内容如下
server {
    listen       80;
    server_name  cfmall.com;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
	  # x为主机ip,8200为cfmall-product服务端口,
      proxy_pass http://x.x.x.x:8200; 
    }
    #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;
    }
}重启nginx:docker restart nginx
测试

附录
给配置文件添加行数显示:
ESC  ---> :set number