老师,出大问题了!帮我看一哈!!!
来源:4-2 Nginx安装和基础代理配置

guaguaerhao
2019-10-17
1、nignx的配置如下:
目录:/usr/local/etc/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include servers/*.conf; # 我改了这里
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
目录:/usr/local/etc/servers/test.conf
server {
listen 80;
server_name test.com;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
}
}
应用目录如下
index.html 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<img src="test.jpg"/>
</body>
</html>
server.js 代码
const http = require('http')
const path = require('path')
const fs = require('fs')
const url = require('url')
http.createServer((req,res)=>{
const pathname = url.parse(req.url).pathname
const html = fs.readFileSync(path.join(__dirname, 'index.html'))
const img = fs.readFileSync(path.join(__dirname, 'test.jpg'))
if (pathname === '/') {
res.writeHeader(200, {
'Content-Type': 'text/html'
})
res.write(html)
res.end()
} else {
res.writeHeader(200, {
'Content-Type': 'image/jpeg'
})
res.write(img)
res.end()
}
}).listen(8888)
还有hosts文件
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
2、运行应用
使用sudo brew services start nginx启动nginx,以及nodemon server 启动应用,均正常。
2.1、访问localhost,浏览器显示如下图
2.2、访问localhost:8888,浏览器显示如下图
2.3、接下来访问127.0.0.1,浏览器显示如下图
2.4、但是访问test.com的时候显示如下图
问题写到这里,突然意识到会不会是因为test.com的问题,然后我就修改了test.conf的配置中的
test.com 改成 test1.com
(有重启nginx)
但是修改为test1.com之后,问题并没有解决,访问test1.com,浏览器显示由2.4图变成了下图
老师,你知道我哪里出问题了吗??
而且我有一个疑惑,localhost:80访问到nginx的默认html,
但是127.0.0.1:80就访问去了nodemon启动的应用(是因为ngnix代理成功了,但是test1.com却未能访问nodemon启动的应用?)。
写回答
1回答
-
你的hosts里面没有配test.com啊,你这个么访问就访问到外网了,当然访问不到
012019-10-21
相似问题