Nginx - 域上的WordPress和子目录中的Node.js应用程序

时间:2017-08-14 02:32:50

标签: node.js wordpress express nginx https

问题:我在https://example.com/有一个现有的WordPress网站。我有一个在localhost:9001上运行的Node.js应用程序,我想在https://example.com/subdirectory访问它。

请求:请帮我弄清楚我需要在Nginx服务器块和/或app.js文件中放置什么才能使其正常工作。

为了简洁起见,我没有包括我尝试过的所有事情,但我可以说在过去的几天里我尝试过很多事情而没有成功。非常感谢任何帮助!

以下是我的相关文件和信息:

Node.js app目录结构

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;

routes.js

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;
    }
}

1 个答案:

答案 0 :(得分:1)

类似的东西:

location /subdirectory/  {
    proxy_pass http://127.0.0.1:9001/;
 }

/subdirectory/foo.html的请求代理到127.0.0.1:9001/foo.html

The nginx proxy-pass documentation详细介绍了使用尾部斜杠和URI替换的行为。