• 配置nginx动静分离全步骤


    1、目的

    高并发环境下,大量请求从服务器后端获取资源,给后台服务器造成了巨大的压力。因此,在发布代码时,需要将后端的静态资源放到nginx,形成动态文件和静态文件分离。静态文件就从nginx服务器上获取,动态文件就从后端服务器获取。

    2、服务器加载静态资源的链路方式

    (1)用户请求接口:

    可以在hosts文件夹下设置:

    192.168.52.130 gulimall.com

    IP地址:代表nginx所在的服务器ip

    域名代表要访问的接口;

    (2)192.168.52.130服务器,接收到了80端口的请求,需要对gulimall.com这个域名进行处理。

    配置文件如下:

    1. [root@localhost conf]# vim nginx.conf
    2. user nginx;
    3. worker_processes 1;
    4. error_log /var/log/nginx/error.log warn;
    5. pid /var/run/nginx.pid;
    6. events {
    7. worker_connections 1024;
    8. }
    9. http {
    10. include /etc/nginx/mime.types;
    11. default_type application/octet-stream;
    12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    13. '$status $body_bytes_sent "$http_referer" '
    14. '"$http_user_agent" "$http_x_forwarded_for"';
    15. access_log /var/log/nginx/access.log main;
    16. sendfile on;
    17. #tcp_nopush on;
    18. keepalive_timeout 65;
    19. #gzip on;
    20. upstream gulimall{
    21. server 10.49.103.92:88; ##对gulimall进行处理,路由到服务器的网关
    22. }
    23. server{ ##解析域名,映射到80端口
    24. listen 80;
    25. server_name gulimall.com *.gulimall.com;
    26. location / {
    27. proxy_set_header Host $host;
    28. proxy_pass http://gulimall;
    29. }
    30. }
    31. include /etc/nginx/conf.d/*.conf;
    32. }

     

     cp default.conf gulimall.conf

    cat gulimall.conf

    1. server {
    2. listen 80;
    3. server_name gulimall.com *.gulimall.com;
    4. #charset koi8-r;
    5. #access_log /var/log/nginx/log/host.access.log main;
    6. location /static/{
    7. root /usr/share/nginx/html;
    8. }
    9. location / {
    10. proxy_set_header Host $host;
    11. proxy_pass http://gulimall;
    12. }
    13. #error_page 404 /404.html;
    14. # redirect server error pages to the static page /50x.html
    15. #
    16. error_page 500 502 503 504 /50x.html;
    17. location = /50x.html {
    18. root /usr/share/nginx/html;
    19. }
    20. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    21. #
    22. #location ~ \.php$ {
    23. # proxy_pass http://127.0.0.1;
    24. #}
    25. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    26. #
    27. #location ~ \.php$ {
    28. # root html;
    29. # fastcgi_pass 127.0.0.1:9000;
    30. # fastcgi_index index.php;
    31. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    32. # include fastcgi_params;
    33. #}
    34. # deny access to .htaccess files, if Apache's document root
    35. # concurs with nginx's one
    36. #
    37. #location ~ /\.ht {
    38. # deny all;
    39. #}
    40. }

    docker restart nginx;

    1、gulimall.com,因为在本地配置了访问路径,访问路径为nginx的ip地址。所以就不需要由域名服务器进行解析,而是直接访问nginx的ip和80端口。

    2、又因为gulimall.com的网关在nginx.conf和gulimall.conf进行了配置,所以由nginx路由到了微服务的网关模块,再根据微服务的网关模块的断言配置,进而路由到了微服务的商品服务进行接口的页面的访问。

    3、动静分离的静态资源,也会放在nginx的所在的服务器目录下,根据nginx的配置,进行存放,进而商品服务可以访问到静态资源。

    1. - id: gulimall_host_route
    2. uri: lb://gulimall-product
    3. predicates:
    4. - Host=gulimall.com,item.gulimall.com

    经过gateway网关,路由到了gulimall-product服务,gulimall-product的服务,再从nginx进行获取静态文件,而不用从后端服务器获取。

    nginx服务器存储静态文件的地址:

     gulimll-product的html里边只需要:

    
    即可获取到nginx的静态资源。
  • 相关阅读:
    Mac OS 使用Metal渲染NV12、YUV420、CMSampleBufferRef视频
    1 分钟 Serverless 搭建你的首个个人网站(完成就送猫超卡)
    postman中文乱码
    vue+element 树形结构 改成懒加载模式(原理element有),这里只做个人理解笔记
    控制游戏人物移动的细节到底有多少?
    vue项目打包成H5apk中使用语音播放
    华为机试 - 处理器问题
    (附源码课件)10款Java小游戏满足你各种需求
    新研一暑假计划
    MySQL之DQL语句
  • 原文地址:https://blog.csdn.net/pshdhx/article/details/126619937