gpt4 book ai didi

c++ - nginx/FastCGI 应用程序每次请求多次加载页面

转载 作者:行者123 更新时间:2023-11-28 07:36:50 25 4
gpt4 key购买 nike

我正在关注 this tutorial ,我稍微修改了代码并添加了一个计数器。该页面显示“Hello World”,然后显示计数器。现在,奇怪的是,代码编译正常,但每次刷新页面时计数都会增加 2!

我正在生成这样的进程:spawnfcgi.exe -a 127.0.0.1 -p 8000 -f myapp.exe

一切加载正常,但每次刷新页面时,计数再次从 0 跳到 2 再到 4 等。如果我要将一行写入文件,同一行将被写入多次,每次加载网页时都会增加。我的问题是,这是 FastCGI 应用程序的预期效果吗?还是我的代码和/或 FastCGI/nginx 配置中存在某种错误?

main.cpp( Hello World )

#include <iostream>
#include "fcgio.h"

using namespace std;

int main(void)
{
int gcount = 0;

// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();

FCGX_Request request;

FCGX_Init();
FCGX_InitRequest(&request, 0, 0);

while (FCGX_Accept_r(&request) == 0)
{
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);

cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);

cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello, World!</h1>\n"
<< gcount++
<< " </body>\n"
<< "</html>\n";

// Note: the fcgi_streambuf destructor will auto flush
}

// restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);

return 0;
}

nginx.conf

#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}

http {
server {
listen 80;
server_name localhost;

location / {
fastcgi_pass 127.0.0.1:8000;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}
}
}

fastcgi.conf

#fastcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

最佳答案

你能展示一下你的 nginx.conf 吗?

您的浏览器是 chrome 还是类似 chronium 的浏览器?

关于c++ - nginx/FastCGI 应用程序每次请求多次加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16630924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com