修改 Windows hosts 文件

位置:C:\Windows\System32\drivers\etc
在后面追加以下内容:

  1. # guli mall #
  2. 192.168.163.131 gulimall.com

Nginx 配置文件

image.png

分析Nginx配置文件

cat /mydata/nginx/conf/nginx.conf

  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. }

可以看到,在 http 块中最后有 include /etc/nginx/conf.d/*.conf; 这句配置说明在 conf.d 目录下所有 .conf 后缀的文件内容都会作为 nginx 配置文件 http 块中的配置。这是为了防止主配置文件太复杂,也可以对不同的配置进行分类。
下面我们参考 conf.d 目录下的配置,来配置 gulimall 的 server 块配置

gulimall.conf

默认配置下,我们访问 gulimall.com 会请求 nginx 默认的 index 页面,现在我们要做的是当访问 gulimall.com 的时候转发到我们的商品模块的商城首页界面。

查看Windows ip

打开cmd 输入 ipconfig
image.png
这里的 192.168.17.1 和 192.168.163.1 也是 Windows 的本机地址
所以我们配置当访问 nginx /请求时代理到 192.168.163.1:10000 商品服务首页

配置代理

  1. server {
  2. listen 80;
  3. server_name gulimall.com;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/log/host.access.log main;
  6. location / {
  7. proxy_pass http://192.168.163.1:10000;
  8. }
  9. #error_page 404 /404.html;
  10. # redirect server error pages to the static page /50x.html
  11. #
  12. error_page 500 502 503 504 /50x.html;
  13. location = /50x.html {
  14. root /usr/share/nginx/html;
  15. }
  16. }

图示

image.png

反向代理:nginx 代理网关由网关进行转发

1. 修改 nginx.conf

vim /mydata/nginx/conf/nginx.conf
修改 http 块,配置上游服务器为网关地址

  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. upstream gulimall {
  20. server 192.168.163.1:88;
  21. }
  22. include /etc/nginx/conf.d/*.conf;
  23. }

2. 修改 gulimall.conf

配置代理地址为上面配置的上游服务器名

  1. server {
  2. listen 80;
  3. server_name gulimall.com;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/log/host.access.log main;
  6. location / {
  7. proxy_set_header Host $host;
  8. proxy_pass http://gulimall;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. }

image.png

访问跳转分析

当前通过域名的方式,请求 gulimal.com ;

  1. 根据 hosts 文件的配置,请求 gulimall.com 域名时会请求虚拟机 ip

    1. 192.168.163.131 gulimall.com
  2. 当请求到 192.168.163.131:80 时,会被 nginx 转发到我们配置的 192.168.163.1:10000 路径,该路径为运行商品服务的 windows 主机 ip 地址,至此达到通过域名访问商品服务的目的。

    1. server {
    2. listen 80;
    3. server_name gulimall.com;
    4. location / {
    5. proxy_pass http://192.168.163.1:10000;
    6. }
    7. }

    后面的跳转分析

    之后为了统一管理我们的各种服务,我们将通过配置网关作为 nginx 转发的目标。最后通过配置网关根据不同的域名来判断跳转对应的服务。
    Nginx-搭建域名访问环境 - 图5