try*_*ode 3 c cgi fastcgi nginx
这应该是我关于 FastCGI 和 NGINX 的最后一个问题(我已经问了太多了),但是我有一个关于在 Web 服务器上运行 C FastCGI 脚本的问题,特别是 NGINX。现在我的 nginx.conf 文件是这样的:
user nobody nobody;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
location / {
root /nginx/html;
index index.html index.htm new.html;
autoindex on;
fastcgi_pass 127.0.0.1:8000;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个简单的 C FastCGI 脚本,可以打印出 Hello World。我知道为了运行这个脚本,我首先必须编译 C 脚本,这将生成一个二进制文件。然后我使用spawn-fcgi -p 8000 -n <binary>
or执行这个二进制文件cgi-fcgi -start -connect localhost:8000 ./<binary>
。我已经成功地做到了这一点并显示了正确的结果。然而,当我这样做时,CGI 脚本是 Web 服务器上唯一显示的内容。我无法访问任何 .html 页面或任何其他页面。即使我输入随机扩展名(这会导致 404 Page not Found 错误),也会显示 CGI 脚本。基本上我试图让index.html成为主页,然后当用户单击按钮时,用户将被带到一个显示C CGI脚本的新页面。
这可能吗?如果是这样,我该怎么办?我花了几个小时试图在网上寻找解决方案,但没有成功。如果问题太模糊/不清楚或者您需要更多信息,请告诉我!谢谢你!
我能想到有两种可能性。您可以为 CGI 程序分配一个 URI,并使用它来访问它。或者您可以将任何无效的 URI 发送到您的 CGI 程序。
在第一种情况下,您可以使用:
root /nginx/html;
index index.html index.htm new.html;
location / {
try_files $uri $uri/ =404;
}
location /api {
fastcgi_pass 127.0.0.1:8000;
}
Run Code Online (Sandbox Code Playgroud)
因此,任何以 开头的 URI 都/api
将被发送到 CGI 程序。其他 URI 将由 提供服务nginx
,除非未找到,在这种情况下将返回 404 响应。
在第二种情况下,您可以使用:
root /nginx/html;
index index.html index.htm new.html;
location / {
try_files $uri $uri/ @api;
}
location @api {
fastcgi_pass 127.0.0.1:8000;
}
Run Code Online (Sandbox Code Playgroud)
因此任何不存在的 URI 都会被发送到 CGI 程序。
请参阅本文档以了解该try_files
指令,并参阅此文档以了解该location
指令。
归档时间: |
|
查看次数: |
2255 次 |
最近记录: |