运行 使用 NGINX 的 C FastCGI 脚本
Running a C FastCGI script using NGINX
这应该是我关于 FastCGI 和 NGINX 的最后一个问题(我已经问了太多),但我有一个关于 运行在网络服务器上使用 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;
}
}
}
我有一个简单的 C FastCGI 脚本,可以打印出 Hello World。我知道为了 运行 这个脚本,我首先必须编译生成二进制文件的 C 脚本。然后我使用 spawn-fcgi -p 8000 -n <binary>
或 cgi-fcgi -start -connect localhost:8000 ./<binary>
执行这个二进制文件。我成功地做到了这一点并显示了正确的结果。但是,当我这样做时,CGI 脚本是唯一显示在 Web 服务器上的东西。我无法访问任何 .html 页面或任何其他页面。即使我输入了一个随机扩展名,它应该会导致 404 Page not Found 错误,CGI 脚本也会被显示。基本上我试图让 index.html 成为主页,然后当用户单击按钮时,用户将被带到一个显示 C CGI 脚本的新页面。
这可能吗?如果是这样,我该怎么做?我花了几个小时试图在网上找到解决方案,但没有成功。如果问题太过 vague/unclear 或者您需要更多信息,请告诉我!谢谢!
我能想到的有两种可能。您可以为您的 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;
}
因此,任何以 /api
开头的 URI 都将被发送到 CGI 程序。 nginx
将提供其他 URI,除非未找到,在这种情况下将返回 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;
}
所以任何不存在的 URI 都会被发送到 CGI 程序。
有关 location
指令,请参阅 this document for the try_files
directive, and this document。
这应该是我关于 FastCGI 和 NGINX 的最后一个问题(我已经问了太多),但我有一个关于 运行在网络服务器上使用 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;
}
}
}
我有一个简单的 C FastCGI 脚本,可以打印出 Hello World。我知道为了 运行 这个脚本,我首先必须编译生成二进制文件的 C 脚本。然后我使用 spawn-fcgi -p 8000 -n <binary>
或 cgi-fcgi -start -connect localhost:8000 ./<binary>
执行这个二进制文件。我成功地做到了这一点并显示了正确的结果。但是,当我这样做时,CGI 脚本是唯一显示在 Web 服务器上的东西。我无法访问任何 .html 页面或任何其他页面。即使我输入了一个随机扩展名,它应该会导致 404 Page not Found 错误,CGI 脚本也会被显示。基本上我试图让 index.html 成为主页,然后当用户单击按钮时,用户将被带到一个显示 C CGI 脚本的新页面。
这可能吗?如果是这样,我该怎么做?我花了几个小时试图在网上找到解决方案,但没有成功。如果问题太过 vague/unclear 或者您需要更多信息,请告诉我!谢谢!
我能想到的有两种可能。您可以为您的 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;
}
因此,任何以 /api
开头的 URI 都将被发送到 CGI 程序。 nginx
将提供其他 URI,除非未找到,在这种情况下将返回 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;
}
所以任何不存在的 URI 都会被发送到 CGI 程序。
有关 location
指令,请参阅 this document for the try_files
directive, and this document。