我在使用Node.js和PHP配置nginx时遇到问题 基本上我想要这样的东西:
所以我想创建类似PHP服务器的东西,node.js在后台工作以完成一些特殊任务。我不希望子域上的节点服务器,我需要它一直运行而不是特定的请求。
我的nginx配置
server {
listen *:80;
server_name my-project.com www.my-project.com;
client_max_body_size 1m;
root /var/www/public;
index index.html index.htm index.php;
access_log /var/log/nginx/nxv_5rxici0o7b9k.access.log;
error_log /var/log/nginx/nxv_5rxici0o7b9k.error.log;
location / {
proxy_pass http://localhost:3001; ### I added this line
root /var/www/public;
try_files $uri $uri/ /index.php$is_args$args;
autoindex off;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/public;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
}
sendfile off;
}
server.js
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
app.get('/', function (req, res) {
console.log("TEST!!");
proxy.web(req, res, { target: 'http://127.0.0.1:80' });
//res.send('Hello World!');
});
app.listen(3001);
有了这个,我得到内部服务器错误可能是因为我进入循环,将我重定向到nginx然后重定向到节点服务器等等。
任何想法如何让我的节点服务器在my-project.com上运行而不是nginx?
答案 0 :(得分:0)
我不是sur(我从nginx开始),但你没有添加第二个配置服务器到127.0.0.1和localhost只为php服务?
server {
listen *:80;
server_name 127.0.0.1 localhost;
client_max_body_size 1m;
root /var/www/public;
index index.html index.htm index.php;
access_log /var/log/nginx/nxv_5rxici0o7b9k.access.log;
error_log /var/log/nginx/nxv_5rxici0o7b9k.error.log;
location ~ \.php$ {
root /var/www/public;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
}
sendfile off;
}
server {
listen *:80;
server_name my-project.com www.my-project.com;
client_max_body_size 1m;
root /var/www/public;
index index.html index.htm index.php;
access_log /var/log/nginx/nxv_5rxici0o7b9k.access.log;
error_log /var/log/nginx/nxv_5rxici0o7b9k.error.log;
location / {
proxy_pass http://localhost:3001; ### I added this line
root /var/www/public;
try_files $uri $uri/ /index.php$is_args$args;
autoindex off;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/public;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
}
sendfile off;
}