我有一个配置了nginx的Rails应用程序,其根目录位于/ var / www / apps / example / current / public。它是在https://www.example.com访问的。一切都很好。我决定添加一个博客,我想和Wordpress一起去。我将它安装在服务器上的另一个目录/ var / www / apps / blog中,我希望能够通过转到https://www.example.com/blog
来访问它以下是我的nginx配置:
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/certs/example.com.pem;
ssl_certificate_key /etc/ssl/certs/example.com.key;
server_name www.example.com;
root /var/www/apps/example/current/public;
passenger_enabled on;
rails_env production;
client_max_body_size 100M;
client_body_buffer_size 256k;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /blog {
root /var/www/apps;
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
我可以导航到example.com/blog,它带我到那里没有任何问题。当我启用漂亮的URL并尝试查看帖子时,问题就出现了。它绕过/ blog目录并点击我的Rails应用程序,结果是404.我在/var/log/nginx/error.log中看到以下错误:
2016/06/30 17:24:32 [error] 7035#0: *20 "/var/www/apps/blog/hello-world/index.html" is not found (2: No such file or directory), client: 199.27.128.198, server: www.example.com, request: "GET /blog/hello-world/ HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/blog/"
添加index index.php
指令并不能解决问题,它只会说明找不到/var/www/apps/blog/hello-world/index.php
。
有什么想法吗?我很难过。
答案 0 :(得分:1)
var http = require("http");
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE html>");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World Page</title>");
response.write("</head>");
response.write("<body>");
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('test.db');
var data = [];
db.serialize(function() {
db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT * FROM lorem", function(err, row) {
data.push(row);
}, function() {
response.write(JSON.stringify(data));
response.write("</body>");
response.write("</html>");
response.end();
});
});
db.close();
});
server.listen(process.env.PORT || 1337);
对于非常永久的链接是不够的。
要启用非常永久链接,您需要将其更改为:
try_files $uri=404;
或者,试试non-root try_files redirect:
location /blog {
root /var/www/apps/blog;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
答案 1 :(得分:0)