学习码头和码头-写作,遇到一个粘合者:
这是我的docker-compose.yml文件:
version: '3'
services:
site:
build:
context: "."
dockerfile: "Dockerfile-site-dev"
environment:
- "HTTPS_METHOD=noredirect"
volumes:
- "./site/:/usr/share/nginx/html"
ports:
- "8080:80"
app:
build:
context: "."
dockerfile: "Dockerfile-server-dev"
environment:
- "HTTPS_METHOD=noredirect"
volumes:
- "./app/:/app/"
ports:
- "3000:3000"这将在高寒图像中实例化nginx web前端和nodejs/express后端。
问题是,当我从网页触发“get”到“http://app:3000/service”到nodejs应用程序容器时,它会重定向到https (失败是因为没有在容器上设置https --这是一个特殊的内部测试& http只是好的)。
已经尝试了jQuery$.get和axios.get --同样的结果。
我可以执行到‘站点’容器和ping‘应用’很好,如果我卷曲'http://app:3000/testme‘从’站点‘容器(这只是返回一个“我在这里!”(反应)它工作得很好。
但是当我从一个页面执行它的时候,有东西在强迫307。
我在nginx配置中没有看到任何重定向(无论如何,这只会影响页面访问),而且我的nodejs应用程序代码中没有任何东西可以触发重定向。
好像码头上有东西在强迫重定向。
注意,在docker文件中,我在两个容器上设置了环境"HTTPS_METHOD=noredirect“,这似乎没有任何效果。
任何见解都值得赞赏。
增加:
Dockerfile-site-dev:
FROM nginx:stable-alpine
WORKDIR /usr/share/nginx/htmlDockerfile-app-dev:
FROM node:10.13-alpine
WORKDIR /app
RUN yarn global add nodemon
CMD ["sh","-c", "yarn install && nodemon ./index.js"]在站点端,nginx配置是来自高山基础图像的香草。
下面是触发对应用服务器的请求的index.html:
<!DOCTYPE html>
<html>
<head>
<title>Vote SSE Demo</title>
<style>
body {font-family:Arial, Helvetica, sans-serif;
text-align:center;}
#yes,#no,#message {font-size: 1.5em;}
button {border-radius:0.5em;background-color:lightgray;}
</style>
</head>
<body>
<h1>Will "Hello, World!" bring world peace?</h1>
<div><button id="yes">Yes</button></div><br>
<div><button id="no">No</button></div>
<br/><br/>
<div id="message"></div>
</body>
<script src="./jquery-2.1.4.min.js"></script>
<script src="./axios.min.js"></script>
<script>
function vote(yes) {
console.log('voting: ' + (yes? 'yes' : 'no'));
axios.get("http://app:3000/vote?yes=" + yes)
.then(function(rsp) {
$("#yes").hide();
$("#no").hide();
$("#message").text("Thank you for your vote!");
})
.catch(function(err) {
console.log('Axios get error: ' + err);
});
}
$("#yes").on("click", function(){
vote(true);
});
$("#no").on("click", function() {
vote(false);
});
</script>
</html>对于应用程序端,下面是package.json文件:
{
"name": "callbacks",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.16.4"
}
}下面是index.js:
const express = require('express');
const sse = require('./sse');
const app = express()
let connections = [],
votes = {yes: 0, no: 0};
app.use(sse);
app.get('/vote', function(req, res) {
console.log('server in vote...');
let i;
if (req.query.yes === 'true')
votes.yes++;
else
votes.no++;
for (i = 0; ix < connections.length; ++i) {
connections[i].sseSend(votes);
}
res.sendStatus(200);
});
app.get('/stream', function(req, res) {
res.sseSetup();
res.sseSend(votes);
connections.push(res);
});
app.get('/testme', function(req, res) {
console.log('server: in testme');
res.send("I'm here!!!\n");
})
app.listen(3000);
console.log('Listening on port 3000');SSE是ServerEvent,这就是我要测试的。
不然的话,一切都很基本。
我确实认为docker是可疑的,因为我可以从站点容器中卷曲这个应用程序,没有问题(或者这就是它不值得怀疑的原因……)
发布于 2018-11-17 00:32:50
html在浏览器上运行(在docker之外)。
浏览器不知道任何“应用程序”主机。
因此,在html中,替换:
axios.get("http://app:3000/vote?yes=" + yes)使用
axios.get("http://localhost:3000/vote?yes=" + yes)https://stackoverflow.com/questions/53344757
复制相似问题