如何配置 nginx 以使用 Node.js 和 PHP
How to configure nginx to work with Node.js and PHP
我在配置nginx以使用Node.js和PHP时遇到问题。
基本上我想要这样的东西:
- 用户打开 my-project.com
- 节点.js服务器在端口 3001 上运行
- 来自 node.js 的请求通过 http-proxy 发送到端口 80 上的 my-project.com
- nginx(端口80)服务器运行PHP脚本并向用户显示输出
所以我想用node创建类似PHP服务器的东西.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;
}
服务器.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上运行吗?
我不是 sur(我以 nginx 开头),但您不会将第二个配置服务器添加到 127.0.0.1 和本地主机以仅提供 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;
}
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 如何使用 node.js 比较两个 json 数组
- Node.js's Buffer.writeFloatBE in Javascript
- node.js:setInterval()正在跳过调用
- 如何处理node.js节点mongodb中的连接和查询队列
- Node.js正在更改应用程序以使用集群模块
- 如何使用Node.js最有效地解析网页
- 在openshift node js应用程序中获取请求
- 正在删除node.js中已验证的网站
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 在索引.html和应用.js [node.js] 之间共享变量
- Node.js中的JavaScript原型对象效率
- node.js请求数据事件未在CORS ajax调用中触发
- 使用node.js制作网站
- 使用node.js服务器中的evernote api获取note内容
- 有没有任何方法可以使用node-js从不同的机器打开浏览器
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 在Node.js中上传和检索图像
- 什么's本地node.js服务器和python简单http服务器之间的区别
- 如何使用 Node JS 获取 JSON 嵌套数组值