[云盘]Nginx集成FastCGI

本文介绍了如何在Nginx上部署FastCGI以处理动态网页。首先讲解了Nginx作为web服务器的优势,然后阐述了spawn-fcgi的作用,即作为Nginx与FastCGI之间的通信代理。详细说明了spawn-fcgi的使用方法,包括指定IP、端口和FastCGI程序。最后,探讨了Nginx集成FastCGI的配置过程,特别指出在nginx.conf中设置location模块以转发请求到FastCGI进程。
摘要由CSDN通过智能技术生成

Nginx

Ngixn作为 web服务器具有简单部署,效率高,热部署等等优点,但是在Nginx上只能部署静态网页,对于动态网页需要调用CGI程序去处理

spawn-fcgi

spawn-fcgi作为的是ngixn和fastcgi的代理实现两者之间通信

在这里插入图片描述

使用

  • 编写fcgi程序
  • spawn-fcgi -a IP -p 端口 -f fastcgi程序
    • -a - IP: 服务器IP地址
    • -p - port: 服务器将数据发送到的端口
    • -f - cgi程序: spawn-fcgi启动的可执行fastcgi程序

Nginx集成FastCGI

  • Nginx集成FastCGI只需要在nginx.conf中http模块中的虚拟主机配置模块server中,添加location模块,指定请求指令

#监听用户的test请求,通过fastcgi_pass交给本地8001端口处理

			#此时spwan-cgi已经将8001端口交给之前我们写好的test进程处理
			location /test {
				fastcgi_pass 127.0.0.1:8001;//指定spawn-fcgi代理的fcgi程序监听的ip+port
				fastcgi_index test.cgi;//默认执行的fcgi程序
				include fastcgi.conf;//需要用到fcgi环境变量,要包含进来
			}
  • 配置文件代码
    单个服务器的配置如下所示

      nginx.conf
      	
      #运行用户, 如果centos平台,需要设置 user root;
      #user  nobody;
      
      #启动进程,通常设置成和cpu的数量相等
      worker_processes  1;
      
      pid        logs/nginx.pid;
      
      #工作模式及连接数上限
      events {
      	#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
      	#仅用于linux2.6以上内核,可以大大提高nginx的性能
      	use epoll;
      	
      	#单个后台worker process进程的最大并发链接数
          worker_connections  1024;
      }
      
      
      http {
      	#设定mime类型,类型由mime.type文件定义
          include       mime.types;
          default_type  application/octet-stream;
      	
      	#上传文件大小限制设置
      	client_max_body_size 30m;
      
          sendfile        on;
          #tcp_nopush     on;
      
      	#连接超时时间
          #keepalive_timeout  0;
          keepalive_timeout  65;
      	
      	#开启gzip压缩
          #gzip  on;
      	
      
      
      
      	#设定虚拟主机配置
          server {
      		#侦听80端口
              listen       80;
      		
      		#也可以设置为 合法域名
              server_name  localhost;
      		
      		#定义服务器的默认网站根目录位置
              #root   html;
      
      		#默认请求
              location / {
      			#配置反向代理功能
      			#proxy_pass http://mike.com;
      
      			#定义如果访问根目录的请求目录
      			root   html;
      			#定义首页索引文件的名称
      			index index.html;
              }
      
      		location /group1/M00 {
      			root /usr/fastdfs/fastdfs0/data;
      			ngx_fastdfs_module;
      		}
      
      		#监听用户的test请求,通过fastcgi_pass交给本地8001端口处理
      		#此时spwan-cgi已经将8001端口交给之前我们写好的test进程处理
      		location /test {
      			fastcgi_pass 127.0.0.1:8001;
      			fastcgi_index test;
      			include fastcgi.conf;
      		}
      		
      		location /echo {
                  fastcgi_pass 127.0.0.1:8002;
                  fastcgi_index echo.cgi;
                  include fastcgi.conf;
              }
      
      		#登陆cgi
      		location =/login {
                  fastcgi_pass 127.0.0.1:8881;
                  fastcgi_index login.cgi;
                  include fastcgi.conf;
              }
      		
      		#注册cgi
      		location =/reg {
                  fastcgi_pass 127.0.0.1:8882;
                  fastcgi_index reg.cgi;
                  include fastcgi.conf;
              }
      		
      		#md5秒传cgi
      		location =/md5 {
                  fastcgi_pass 127.0.0.1:8883;
                  fastcgi_index md5.cgi;
                  include fastcgi.conf;
              }
      		
      		#上传文件cgi
      		location =/upload {
                  fastcgi_pass 127.0.0.1:8884;
                  fastcgi_index upload.cgi;
                  include fastcgi.conf;
              }
      		
      		#用户文件列表展示cgi程序
      		location =/myfiles {
                  fastcgi_pass 127.0.0.1:8885;
                  fastcgi_index myfiles.cgi;
                  include fastcgi.conf;
              }
      		
      		#分享、删除文件、pv字段处理cgi程序
      		location =/dealfile {
                  fastcgi_pass 127.0.0.1:8886;
                  fastcgi_index dealfile.cgi;
                  include fastcgi.conf;
              }
      		
      		#共享文件列表展示cgi程序
      		location =/sharefiles {
                  fastcgi_pass 127.0.0.1:8887;
                  fastcgi_index sharefiles.cgi;
                  include fastcgi.conf;
              }
      		
      		#共享文件pv字段处理、取消分享、转存文件cgi程序
      		location =/dealsharefile {
                  fastcgi_pass 127.0.0.1:8888;
                  fastcgi_index dealsharefile.cgi;
                  include fastcgi.conf;
              }
            
      		#定义错误提示页面
      		error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
             
          }
      }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值