本地安装nginx

下载链接

http://nginx.org/en/download....
clipboard.png

nginx结构

clipboard.png

运行nginx

clipboard.png
打开localhost:80

设置代理

server.js
const http = require('http');
const fs = require('fs');
http.createServer(function(req, res) {
    console.log('request come', req.headers.host);
    if (req.url === '/') {
        const html = fs.readFileSync('test.html', 'utf8');
        res.writeHead(200, {
            "Content-Type": "text/html",
        })
        res.end(html)   
    }
}).listen(8888)
console.log('server start at port 8888')
D:nginxnginx-1.15.6confnginx.conf
增加一个server
     server {
        listen       80;
        server_name  test.com;

        location / {
            proxy_pass http://127.0.0.1:8888;
        }
    } 

重启运行nginx
http://localhost:8888/

clipboard.png

test.com

clipboard.png

nginx子文件配置方式

新建conf.d文件夹和test.conf文件

clipboard.png

修改nginx.conf
include   conf.d/*.conf;

clipboard.png

conf.d/test.conf
    server {
        listen       80;
        server_name  test.com;

        location / {
            proxy_pass http://127.0.0.1:8888;
            proxy_set_header Host $host;
        }
    } 

设置host

clipboard.png

server.js打印
clipboard.png

修改nginx.conf

     server {
        listen       80;
        server_name  test.com;

        location / {
            proxy_pass http://127.0.0.1:8888;
            proxy_set_header Host $host;
        }
    } 

server.js打印
clipboard.png


渣渣辉
1.3k 声望147 粉丝