1、查看主配置类

1.1、主配置类

image.png

  1. http {
  2. include /etc/nginx/mime.types;
  3. default_type application/octet-stream;
  4. user nginx;
  5. worker_processes 1;
  6. error_log /var/log/nginx/error.log warn;
  7. pid /var/run/nginx.pid;
  8. events {
  9. #最大连接数
  10. worker_connections 1024;
  11. }
  12. http {
  13. include /etc/nginx/mime.types;
  14. default_type application/octet-stream;
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. access_log /var/log/nginx/access.log main;
  19. sendfile on;
  20. #tcp_nopush on;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. # 配置的负载均衡,分布式不只一台主机
  24. upstream gulimall{
  25. server 192.168.144.1:88;
  26. }
  27. #将server块配置到其他文件并引入
  28. include /etc/nginx/conf.d/*.conf;
  29. }

1.2、主配置类的结构

image.png

2、配置server块

2.1、创建配置文件

  1. cp default.conf gulimall.conf

image.png

2.2、配置server块&添加域名

注意点:nginx转发默认会丢掉很多信息,包括域名Host
image.png
修改配置
image.png

  1. server {
  2. #监听的端口
  3. listen 80;
  4. #监听的域名
  5. server_name gulimall.com;
  6. #charset koi8-r;
  7. #access_log /var/log/nginx/log/host.access.log main;
  8. location / {
  9. #转发到负载均衡组
  10. proxy_pass http://gulimall;
  11. #手动设置丢失的host域名
  12. proxy_set_header Host $host;
  13. }
  14. #error_page 404 /404.html;
  15. # redirect server error pages to the static page /50x.html
  16. #
  17. error_page 500 502 503 504 /50x.html;
  18. location = /50x.html {
  19. root /usr/share/nginx/html;
  20. }

3、网关路由

注意点:这个路由规则写在其他规则之后,不然会覆盖其他路由规则

  1. # 通过域名匹配
  2. - id: gulimal_host_route
  3. uri: lb://gulimall-product
  4. predicates:
  5. - Host=**gulimall.com,gulimall.com