2017-08-14 127 views
-1

问题:我有一个现有的WordPress网站https://example.com/。我有一个运行在localhost:9001上的Node.js应用程序,我想让它可以在https://example.com/subdirectory上访问。Nginx - 一个域中的WordPress和一个子目录中的Node.js应用程序

请求:请帮我弄清楚我需要放在我的Nginx服务器模块和/或app.js文件中才能使它工作。

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

这里是我的相关文件和信息:

的Node.js应用程序的目录结构

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

喜欢的东西:

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对行为细节代换。

+0

哦,我的天啊......我不知道为什么只是这一行工作,但** THANK YOU!** 我已经尝试了各种前缀的符号,和许多不同类型的proxy_的*许多不同的位置块在他们的指令,但不知何故这个简单的你提供的是唯一的工作。 –

相关问题