修改nginx配置,并重启nginx服务

各位读者,大家好。

    我们在很多项目中都是前后集成的方式,但是前端无法直接访问后端,因此使用nginx中间件来实现跳转和负载。

    那么我们就需要会配置nginx。

    如下,先找到前端部署的服务所在机器。登录之后找到nginx配置文件:

   

    比如,我的nginx配置文件在/mnt/nginx/conf/nginx.conf

    第一步,修改配置文件:

   

1)配置(前端调)后端服务的跳转

     例如:假设该机器IP为x.x.x.x,外网可访问,并且前端部署端口为80;

    那么通过本地浏览器访问http://x.x.x.x/goods/queryAll 其实就是访问后端接口 http://10.111.21.15:8668/queryAll

#此处配置根据后端服务名找到服务器ip:port
    upstream goods-service {
        server 10.111.21.15:8668;
        # 如果有多个节点,可以同理配置多个 } #前端所调用的后端服务接口前缀 location
/goods/ { root registry/static_root; index index.html index.htm; proxy_pass http://goods-service/; }

 

2)配置前端静态资源

     例如:假设该机器IP为x.x.x.x,外网可访问,并且前端部署端口为80;

     那么通过本地浏览器访问http://x.x.x.x/title/index.html 其实就是访问部署在该机器中的前端静态资源:./title/index.html

# 后端映射的服务及端口(不存在的端口),此处只是仅作挂靠,并无实际含义
    upstream mobile-gateway-ytt {
        server 10.111.21.15:8686;
        # 如果有多个节点,可以同理配置多个
    }


# 前端静态资源的访问配置
        location /bwmg/ {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt/;
        }
        location /scan {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }
        location /scan/saoma  {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /s {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /t {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /w {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /invoice {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /issue {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }   
        location /MP_verify {
            root        html;
            index       index.html index.htm;
            proxy_pass  http://mobile-gateway-ytt;
        }
        location /title {
            # 连接到资源的路径
            alias      /opt/nginx/static/static_root/title/;
            index       index.html index.htm;   
        }

 

第二步,重启nginx,我的nginx是装在docker容器中的

docker restart nginx

 

第三步,查询nginx启动情况。因为我的nginx是装在docker容器中的

docker ps

 

 最后,如果nginx.conf配置文件中只有upstream,而不存在location,那么肯定是把url的路径映射放在另外一个文件了,通过include进入。例如

 

 

如果对您有帮助,您的鼓励是对作者汗水最大的肯定~

posted @ 2020-04-08 16:11  温柔的星空,让你感动  阅读(6588)  评论(0编辑  收藏  举报