Nginx-域上的WordPress和子目录中的Node.js应用程序
问题:我现有WordPress网站位于。我有一个在localhost:9001上运行的Node.js应用程序,我想让它在 请求:请帮助我找出需要在我的Nginx服务器块和/或app.js文件中放入什么才能使其正常工作 为了简单起见,我没有包括我尝试过的所有事情,但我可以说,在过去的几天里,我尝试了很多事情,但都没有成功。非常感谢您的帮助 以下是我的相关文件和信息: Node.js应用程序目录结构Nginx-域上的WordPress和子目录中的Node.js应用程序,node.js,wordpress,express,nginx,https,Node.js,Wordpress,Express,Nginx,Https,问题:我现有WordPress网站位于。我有一个在localhost:9001上运行的Node.js应用程序,我想让它在 请求:请帮助我找出需要在我的Nginx服务器块和/或app.js文件中放入什么才能使其正常工作 为了简单起见,我没有包括我尝试过的所有事情,但我可以说,在过去的几天里,我尝试了很多事情,但都没有成功。非常感谢您的帮助 以下是我的相关文件和信息: Node.js应用程序目录结构 pwd = /home/user/application/ . ├── app.js ├── fa
pwd = /home/user/application/
.
├── app.js
├── favicon.ico
├── node_modules
├── package.json
├── routes
│ └── routes.js
├── static
│ ├── images
│ ├── scripts
│ └── styles
└── views
└── index.html
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var app = express();
var routes = require('./routes/routes.js');
app.set('ipaddr', '127.0.0.1');
app.set('port', process.env.PORT || 9001);
app.set('views', path.join(__dirname, 'views'));
app.use('/', routes);
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'views')));
app.use(express.static(path.join(__dirname, 'static')));
app.use('styles', express.static(path.join(__dirname + 'static/styles')));
app.use('scripts', express.static(path.join(__dirname + 'static/scripts')));
app.use('images', express.static(path.join(__dirname + 'static/images')));
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.listen(app.set('port'));
module.exports = app;
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.sendFile('index.html');
});
module.exports = router;
routes.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var app = express();
var routes = require('./routes/routes.js');
app.set('ipaddr', '127.0.0.1');
app.set('port', process.env.PORT || 9001);
app.set('views', path.join(__dirname, 'views'));
app.use('/', routes);
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'views')));
app.use(express.static(path.join(__dirname, 'static')));
app.use('styles', express.static(path.join(__dirname + 'static/styles')));
app.use('scripts', express.static(path.join(__dirname + 'static/scripts')));
app.use('images', express.static(path.join(__dirname + 'static/images')));
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.listen(app.set('port'));
module.exports = app;
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.sendFile('index.html');
});
module.exports = router;
nginx.conf
server {
listen 443 ssl;
server_name example.com;
root /var/www/example.com;
ssl on;
ssl_protocols TLSv1.2;
ssl_certificate /etc/nginx/ssl/example.com_cert_chain.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
比如:
location /subdirectory/ {
proxy_pass http://127.0.0.1:9001/;
}
将代理对/子目录/foo.html
的请求到127.0.0.1:9001/foo.html
有关于尾随斜杠和URI替换行为的详细信息。哦,天哪。。。我不知道为什么这一行就行,但谢谢你!我尝试了许多不同的位置块,其中有各种前缀符号,以及许多不同类型的代理指令,但不知何故,您提供的这个简单的块是唯一有效的。