nginx的基本配置


#user  nobody;

#工作进程数量
worker_processes  4;


events {
    #子进程最大连接数
    worker_connections  1024;
}


http {
    #囊括的文件类型
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #长连接多长时间没沟通后断开
    keepalive_timeout  65;
    #服务器选项
    server {
        listen       5173;
        server_name  localhost;
        #路由匹配 
        location  /mobile {
             #路径别名
             alias  html/mobile;
             #查找根路径下的index.html文件 如果没有就查找index.htm文件
             index  index.html index.htm; 
             # $uri是内置的一个变量,代表当前请求的URI,不包括参数部分。
             #上方都没有匹配到则再次匹配 /mobile 没有则匹配/mobile/ 查看是否存在/mobile/文件夹
             # 是的话查找文件夹下的index.html
             #否则直接返回html/mobile/index.html
             try_files $uri $uri/ html/mobile/index.html;
        }  
        location / {
            root  html/pc;
            index index.html index.htm;
             try_files $uri $uri/ html/pc/index.html;
        }

        # location /api {
        #     # 代理
        #     proxy_pass http://backend_server_ip:backend_port;
        #     # 代理主机名设置
        #     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_set_header X-Forwarded-Proto $scheme;
        # }

        #如果所有路由都没有匹配上转入404
        error_page  404      /404.html;
        location = /404.html {
           root   html/404;
           index  index.html index.htm; 
        }    

    }


}

nginx目录

根据不同设备响应不同文件夹中的内容(文件结构同上)


#user  nobody;

#工作进程数量
worker_processes  4;


events {
    #最大子进程数
    worker_connections  1024;
}



http {

    #囊括的文件类型
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #长连接多长时间没沟通后断开
    keepalive_timeout  65;


    #服务器选项
    server {
        listen       80;
        server_name  localhost;
        #路由匹配 
        location / {
            root html/pc;
            if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)'){
                root html/mobile;
            }
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        try_files $uri $uri/ =404;

    }
}

负载均衡

nginx.cong配置


#user  nobody;

#工作进程数量
worker_processes  4;


events {
    #最大子进程数
    worker_connections  1024;
}



http {
    #负载均衡
    upstream project {
        #配置  拥有的服务器主机/域名 + 端口号
        server localhost:3001;
        server localhost:3002;
    }

    #囊括的文件类型
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #长连接多长时间没沟通后断开
    keepalive_timeout  65;


    #服务器选项
    server {
        listen       5173;
        server_name  localhost;
        #路由匹配 
        location / {
            #代理到上面声明的  project项目中 格式为 http://  +  项目名称
            proxy_pass http://project;
            #http版本
            proxy_http_version 1.1;
            #设置http请求头
            proxy_set_header Upgrade $http_upgrade;
            #协议升级 如http升级为websocket
            proxy_set_header Connection 'upgrade';
            #变量 是否显示端口
            #设置主机名   
            # $host 不显示端口 浏览器请求的ip,不显示端口
            # $http_host 端口存在则显示 浏览器请求的ip和端口号
            # $proxy_host 默认80端口不显示,其它显示 被代理服务的ip和端口号

            proxy_set_header Host $host;
            #用于定义条件跳过缓存
            proxy_cache_bypass $http_upgrade;
            #用于避免缓存特定响应
            # proxy_no_cache
        }   

    }
}

配置 nginx 代理服务后
不设置 proxy_set_header Host 时,浏览器直接访问 nginx,获取到的 Host 是 proxy_pass 后面的值,即 $proxy_host 的值。
设置 proxy_set_header Host $host 时,浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息。
设置 proxy_set_header Host h o s t : host:host:proxy_port 时,浏览器直接访问 nginx,获取到的 Host 是 h o s t : host:host:proxy_port 的值。
设置 proxy_set_header Host $http_host 时,浏览器直接访问 nginx,获取到的 Host 包含浏览器请求的 IP 和端口。
设置 proxy_set_header Host $host 时,浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息。此时代码中如果有重定向路由,那么重定向时就会丢失端口信息,导致 404。

开启的服务内容

//服务一
import http from 'http'
import fs from 'fs'

const server = http.createServer((req,res)=>{
    if(req.url == '/'){
        fs.readFile('./src/index.html',(err,data)=>{
            res.end(data)
        })
    }
})


server.listen(3001,()=>{
    console.log(`项目启动在http://localhost:3002端口上`)
})
//服务二

import http from 'http'
import fs from 'fs'


const server = http.createServer((req,res)=>{
    if(req.url == '/'){
        fs.readFile('./src/index.html',(err,data)=>{
            res.end(data)
        })
    }
})

server.listen(3002,()=>{
    console.log(`项目启动在http://localhost:3001端口上`)
})

文件列表结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值