• nginx 配置 同一域名端口下,根据URL 导向不同的项目目录


    我们现在拥有2个项目。但是只有一个域名,通过nginx配置来实现以下url导向不同的项目。

    后台管理台:{域名}/admin

    用户客户端:{域名}/client 

    server {
        listen 8888;
        server_name ****.*****.com;
        root /home/work/***/static/client;
        index index.html;
        autoindex on;
        charset   utf-8;
    
        location ~ /(system|car)/ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_pass http://192.168.1.1:3000;
        }
    
        #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
        location /admin {
            alias /home/work/****/static/admin/;
            expires  1d;
            index index.html;
        autoindex on;
        }
    
        access_log /home/work/****/logs/static_admin_ng_access.log;
    
        location /api/ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://192.168.1.1:3000;
        }
        #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
        location /client {
            alias /home/work/****/static/client/;
            #expires 为用户信息的缓存时间。1d为一天
            expires  1d;
            index index.html;
        autoindex on;
        }
        access_log /home/work/****/logs/static_client_ng_access.log;
    
    }


    在配置这个的时候,遇到一个坑,就是alias 和root 的区别,导致获取的静态文件的获取的路径不对,一直报404;郁闷的很;

    在配置ng 的location 的生活;一般不要加后面的斜杆;然后加上autoindex on; 自动首页;这样就会自动跳转到首页了;


    alias 和 root 的区别; root 的话;location 中的地址会拼接到root后面;alias就直接代替后面的东西


    如:
    location /admin {
    root  /admin/res/;
    index html.html;
    autoindex no;
    }


    location /admin {
    alias /admin/res/;
    index html.html;
    autoindex no;
    }


    访问地址:localhost:8888/admin/res/app.js;

    root实际访问的地址就是: localhost:8888/admin/res/admin/res/app.js

                    也就是说这个实际访问的地址 ./admin/res/admin/res/app.js ;这样根本找不到文件;

    alias 实际的访问地址就是: localhost:8888/admin/res/app.js;

                    访问的地址是  ./admin/res/app.js


      location /admin/{
    root  /admin/res/;
    index html.html;
    autoindex no;
    }
    上面这种配置 localhost:8888/admin  是不能跳转到首页的;
    需要加上斜杆 localhost:8888/admin/  才能跳转到首页


    location /admin{
    root  /admin/res/;
    index html.html;
    autoindex no;
    }


    这种访问的时候: localhost:8888/admin  这样就可以直接访问了;

    配置服务器代理:

    location /api/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://192.168.1.1:3000;
    }


    一定要填写

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://192.168.1.1:3000;
    这4项了;

    现在这样访问的地址就是  http://192.168.1.1:3000/api/...........;

  • 相关阅读:
    移动端重构系列-移动端html页面优化
    response项目的各个写法
    收藏功能的写法
    浅谈文本溢出省略号代表修剪text-overflow
    几种display:table-cell的应用
    -webkit-transform:scale(1.04)放大缩小效果
    自学Python5.6-面向对象三大基本特征_多态
    自学Python5.5-面向对象三大基本特征_继承
    自学Python6.5-内置模块(re、collections )
    CISCO SMARTnet服务和SMB服务技术支持
  • 原文地址:https://www.cnblogs.com/isylar/p/10402340.html
Copyright © 2020-2023  润新知