Stack Overflow на русском Asked on February 20, 2021
/etc/nginx/nginx.cong
user nginx devx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream backend {
server 127.0.0.1:3001;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name sitename.ru;
#_;
root /opt/sitename/server;
#/usr/share/nginx/html;
index index.html;
#access_log /sitename/nginx_logs/access.log;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ~ ^/(images/||javascripts/|fonts/|data/|index.html|robots.txt|humans.txt|favicon.ico) {
root /opt/sitename/server;
}
location /clicked {proxy_pass http://backend;}
location / {
proxy_pass http://backend;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering on;
}
location /s2 {
proxy_pass "http://127.0.0.1:3000";
root /opt/sitename/server/s2;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
www.js (server)
#!/usr/bin/env node
var app = require('../app');
var debug = require('debug')('sitename:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3001');
app.set('port', port);
//app.set('hostname', '11.111.111.11');
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
app.js
//.....
const indexRouter = require('./routes/index');
//.....
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(stylus.middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
У меня на рабочей, локальной машине все идеально на node.js работает, только без nginx. На рабочем сервере(хостинг) сайт запускается, вроде, и node.js нормально, но вот при запросе fetch на /clicked сервера не раьотает, а дает scripts.js:1006 POST http://11.111.111.11/clicked 404 (Not Found)
все файлы и статик, и сервер в корне – /opt/sitename/server: /js, /css, /routes и т.д.
а вот скрипт вызова сервера здесь:
button.addEventListener('click', async function (e) {
await fetch('/clicked', {
method: 'POST',
mode: 'no-cors',
url: `http://http://11.111.111.11:3001`,
headers: {
'Content-Type': 'application/text'
},
body: JSON.stringify({test: 'This is just a test', prio: 1})
})
.then(res => res.json()).then(jsonData => resp.innerText = jsonData.message);
});
пробовал везде подставлять что 11.111.111.11 айпи, что localhost, 127.0.0.1 – все одинаково.
все логично в логах. Не пойму, где я что-то пропустил. Вроде как нод работает, даже с pm2 запустил последний раз.
лог
22.222.22.22 – – [05/Oct/2020:12:24:08 +0000] "POST /auth/facebook/ HTTP/1.1" 404 169 "http://22.222.222.22/" "Mozilla/5.0 (Windows NT
6.2; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"22.222.22.22 – – [05/Oct/2020:12:24:08 +0000] "POST /auth/facebook/ HTTP/1.1" 404 169 "http://22.222.222.22/" "Mozilla/5.0 (Windows NT
6.2; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0" "-"
ошибки
2020/10/05 12:24:00 [error] 40380#0: *14 open()
"/opt/sitename/server/clicked" failed (2: No such file or directory),
client: 22.222.22.22, server: sitename.ru, request: "POST /clicked
HTTP/1.1", host: "22.222.222.22", referrer: "http://22.222.122.22/"2020/10/05 12:24:08 [error] 40380#0: *15
"/opt/sitename/server/auth/facebook/index.html" is not found (2: No
such file or directory), client: 22.222.22.22, server: sitename.ru,
request: "POST /auth/facebook/ HTTP/1.1", host: "22.222.222.22",
referrer: "http://22.222.222.22/"
а вот facebook/index.html удивило, такого вообще быть не должно, наверно, нужно страницу ‘/’ указать при success…
Где здесь мелочь, которую я не заметил, что бы заработало?
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP