GVKun编程网logo

用golang fastcgi与nginx配合写web(golang实现nginx)

17

在本文中,我们将详细介绍用golangfastcgi与nginx配合写web的各个方面,并为您提供关于golang实现nginx的相关解答,同时,我们也将为您带来关于cgifastcgiwsgiisa

在本文中,我们将详细介绍用golang fastcgi与nginx配合写web的各个方面,并为您提供关于golang实现nginx的相关解答,同时,我们也将为您带来关于cgi fastcgi wsgi isapi fastcgi fastcgi for iis nginx fastcgi pass、django-nginx fastcgi->未处理的异常(从Django 1.2.4升级到Django 1.3之后)、FastCgi与使用Nginx Web服务器的PHP-FPM、FastCgi和PHP-FPM使用Nginx Web服务器的有用知识。

本文目录一览:

用golang fastcgi与nginx配合写web(golang实现nginx)

用golang fastcgi与nginx配合写web(golang实现nginx)

1.配置Nginx/FastCGI

This is actually pretty easy. I assume you already have some experience configuring Nginx.conf. (Each install seemingly has different defaults as to the conf file's location,and contents,so I won't go over it here. mine is in /usr/local/etc/Nginx.)

I assume too that you've configured PHP with FastCGI before. If not,you may still understand what's happening.

All you have to do is tell Nginx to pass certain requests,or maybe all of them if you wish,to FastCGI on a certain port. Our Go program will have a FastCGI handler listening on that same port. If you need a reference,my entire server { ... }block looks like this:
server {
listen 80;
server_name go.dev;
root /root/go/src/godev;
index index.html;
#gzip off;
#proxy_buffering off;

location / {
try_files $uri $uri/;
}
location ~ /app.* {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9001;
try_files $uri $uri.html =404;
}
Notice that the server_nameis go.dev. I added this to my hosts file with the loopback IP address,127.0.0.1. So when I type http://go.dev in my browser,it resolves to my own Box and Nginx receives the request.
Also notice that the fastcgi_passport is not the default 9000,but is 9001. I did this because I already have PHP-fpm listening on port 9000. My Go app will listen on 9001.
Further notice that the fastcgi_passis in a location ~ { ... }block such that any page under /appof go.devwill be redirected to my Go program. You can change this path to whatever you'd like or even replace it with the location / { ... }block above if you want your Go program to be executed out of the root of the site.

Let's see it working

Make a .go file (I'm calling mine app.go) with these contents (though I encourage you to study why/how it's working):
package main
import (
"net"
"net/http"
"net/http/fcgi"
)
type FastCGIServer struct{}
func (s FastCGIServer) ServeHTTP(resp http.ResponseWriter,req *http.Request) {
resp.Write([]byte("<h1>Hello,世界</h1>\n<p>Behold my Go web app.</p>"))
}
func main() {
listener,_ := net.Listen("tcp","127.0.0.1:9001")
srv := new(FastCGIServer)
fcgi.Serve(listener,srv)
Here's what's happening: The main() function creates a network listener at localhost on port 9001,which is our FastCGI pass-thru port from the Nginx config. Then we make a new FastCGIServer and serve requests that pop into that port. The fcgi packagehas one function: Serve,which blocks and waits for incoming requests. The interface is defined in the net/httppackage,which is where the ServeHTTP function comes from.
Notice that it receives a ResponseWriterand a Request. From just these,you can write out to your response,and get everything about the incoming request. In this case,all we're doing is writing a simple HTML string.
So,type this in your terminal:
$ go run app.go
Your Go program is Now waiting for requests. If you set up Nginx.conf like mine,go to http://go.dev/appand you should see,in all its glory:


That was pretty easy!
原文链接: http://mwholt.blogspot.com/2013/05/writing-go-golang-web-app-with-nginx.html

cgi fastcgi wsgi isapi fastcgi fastcgi for iis nginx fastcgi pass

cgi fastcgi wsgi isapi fastcgi fastcgi for iis nginx fastcgi pass

诸如nginx apache这些web服务器致力于静态文件的的传输,一些数据需要计算才能获知,计算的程序都在web服务器背后,所以这时web服务器也扮演了反向代理服务器的角色。
既然web服务器和后台计算程序分作两个进程,进程之间的通信必然要遵从一个协议,这个协议就是通用网关协议
cgi = common gateway interface
cgi方式在对于每个http请求,web宿主服务程序都建立新的进程以调用服务器脚本,相应该请求,处理完后结束这个子进程。这就是fork-and-execute模式。所以用cgi方式的服务器有多少连接请求就会有多少cgi子进程,子进程反复加载是cgi性能低下的主要原因。当用户请求数量非常多时,会大量挤占系统的资源如内存,cpu时间等,造成效能低下。

FCGI = Fast CGI

它其实是CGI在具体实现中的的一个变种。FCGI在规范上跟CGI并没有不同,通过减少CGI代理程序和Web宿主服务程序的通信开销。FCGI建立一个独立的FCGI服务程序进程,和Web宿主服务程序进程通信,FCGI服务进程被一旦启动后,自己分配资源、创建线程响应HTTP请求、并决定自身生命周期,从而大大降低了系统为了创建进程而做出的资源开销。现代流行的Web服务器程序,如PHP、ASP.Net,基本都是FCGI的实现。他还是支持分布式的运算,即FastCGI程序可以在网站服务器以外的主机上执行并且接受来自其他网站服务器来的请求。

FastCGI比GI的特点就是后面的计算程序从web服务进程中独立出来,可以常驻内存,可以分布式部署。

SCGI = Simple CGI

它是FCGI在精简数据协议和响应过程后的产物。其设计目的是为了适应越来越多基于AJAX或REST的HTTP请求,而做出更快更简洁的应答。并且SCGI约定,当服务器返回对一个HTTP协议请求响应后,立刻关闭该HTTP连接。所以不难看出,SCGI更加适合于普遍意义上SOA所提倡的“请求-忘记”这种通信模式。

WSGI = Web Server Gateway Interface

当Web Server收到一个请求后,可以通过Socket把环境变量和一个Callback回调函数传给后端Web应用,Web应用在完成页面组装后通过Callback把内容返回给Web Server。这样做的优点有很多:

  • 异步化,通过Callback将Web请求的工作拆解开,可以很方便的在一个线程空间里同时处理多个Web请求。
  • 方便进行各种负载均衡和请求转发,不会造成后端Web应用阻塞。

此协议是Python语言的专利,它定义了一组在Web服务宿主程序和HTTP响应代理程序之间通信的普遍适用的接口。它的产生是因为Python程序员注意到,对于Web框架和Web宿主服务器程序间,有严重的耦合性,比如说,某些框架是针对Apache的mod_python设计的。于是,WSGI就定义了一套非常低级别的接口。常见的Python Web框架都实现了这个协议:如 CherryPy, Django, web.py, web2py, TurboGears, Tornado, Pylons, BlueBream, Google App Engine[dubious – discuss], Trac, Flask, Pyramid,等等.

以上就介绍了cgi fastcgi wsgi,包括了fastcgi方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

django-nginx fastcgi->未处理的异常(从Django 1.2.4升级到Django 1.3之后)

django-nginx fastcgi->未处理的异常(从Django 1.2.4升级到Django 1.3之后)

我刚刚从Django 1.2.4升级到1.3.

我将Nginx与fastcgi结合使用,由于某种原因,每次访问页面时,都会出现此错误:

Unhandled Exception

An unhandled exception was thrown by the application.

任何想法可能是什么问题?

最佳答案
您需要跟踪fastcgi错误日志.我应该在那里有更详细的信息.

FastCgi与使用Nginx Web服务器的PHP-FPM

FastCgi与使用Nginx Web服务器的PHP-FPM

我正在使用本教程在我的新Web服务器上安装Nginx,PHP和MysqL。

本教程使用的是ISPConfig 3,可以select使用FastCgi还是PHP-FPM。

我想知道哪个更好。 在性能和速度方面,哪两个最适合与Nginx一起使用?

顺便说一句,我也有memcached和xcache在我的服务器上启用。

Nginx + PHPFPM:PATH_INFO总是空的

坏的网关与Debian上的Nginx,单声道和fastcgi

Nginx的基本authentication和子文件夹

Nginx,fastcgi PHP Windows,没有指定input文件

在Nginx上安装wordpress – Nginx发送install.PHP

自定义PHP FastCGI接口? (更快?)

Nginx与FastCGI和wordpress的子目录 – “没有指定input文件”

Nginx错误readv()和recv()失败

Hostmonster apache fcgi上的Redmine 2:Rails应用程序无法正常启动

Nginx发送缺less图像的请求到后端

PHP-FPM比旧的FastCGI处理PHP好得多。 从PHP 5.3.3起,PHP-FPM处于核心地位,旧的FastCGI实现不再可用。

我的答案刚刚被投票(在网上相当一段时间),我明白为什么,所以这里是一个为什么PHP-FPM实际上比旧的FastCGI实施更好的列表。

首先,很久以前就知道FastCGI实现在PHP社区中是不好的。 一个页面,可以在https://wiki.PHP.net/ideas/fastcgiwork找到它的文件说&#xFF1A;

在没有额外的“拐杖”的情况下,php-cgi在生产环境中是没有用的(例如lighttpd distribution或PHP-fpm patch的spawn-fcgi)。 这个项目假设整合这种“拐杖”并且扩展php-cgi来支持不同的协议。

守护进程(分离,创建pid文件,设置环境变量,setuid / setgid / chroot)

优雅重启

分离和改进传输层以支持不同的协议

支持SCGI协议

支持HTTP协议的子集

下面是PHP-FPM从http://PHP-fpm.org/about/取得的更好的列表&#xFF1A;

PHP守护进程:pid文件,日志文件, setsid() , setuid() , setgid() , chroot()

流程管理。 能够“优雅地”停止并启动PHP工作,而不会丢失任何疑问。 这允许逐渐更新配置和二进制文件,而不会丢失任何查询。

限制请求可能来自的IP地址。

动态进程数,取决于负载(自适应进程产卵)。

用不同的uid / gid / chroot / environment和不同的PHP.ini选项来启动worker(不需要安全模式)。

记录STDOUT和STDERR 。

如果使用加速器,在发生意外破坏共享内存操作码高速缓存的情况下,可以紧急重新启动所有进程。

如果set_time_limit()失败,则强制完成进程。

附加功能: – 错误标题 – 加速上传支持 – fastcgi_finish_request() – 慢回溯跟踪

一个小的改正:即使在PHP 5.5.x上,PHP FastCGI SAPI仍然可用。

[root@zulu1 ~]# /usr/local/PHP54/bin/php-cgi -v PHP 5.4.17 (cgi-fcgi) (built: Jul 18 2013 05:12:07) copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0,copyright (c) 1998-2013 Zend Technologies

在fastcgi方面:

Fastcgi更容易监控:fastcgi中每个用户有一个pid。 在具有多个帐户的Web服务器上,很容易找到过载的过程。 另一方面,PHP-fpm根据请求+一个主进程创建很多进程。 当你从不同的IP有很多的连接时,这是混乱的。

Fastcgi配置更容易:Fastcgi包含在Apache配置中。 所以它使事情变得更容易。 另一方面,PHP-fpm需要额外的配置文件。 如果存在配置依赖关系,则会使事情变得复杂。

大共享的托管公司仍然不使用PHP-fpm在2015年与PHP 5.6今天,所有最大的共享网络托管公司(bluehost,hostgator,namecheap)使用fastcgi,但他们不使用PHP-fpm,我认为有为什么他们不提供PHP-fpm作为PHP-handler。

在PHP-fpm方面:

我已经注意到,PHP-fpm比fastcgi消耗的少了20%,比mod_PHP少了20%。 所以,PHP-fpm对于ram数量最少的web服务器来说是很好的选择。

FastCgi和PHP-FPM使用Nginx Web服务器

FastCgi和PHP-FPM使用Nginx Web服务器

我正在使用这个tutorial在我的新的Web服务器上安装Nginx,PHP和MysqL.

本教程正在使用ISPConfig 3,并且有一个选项是使用FastCgi还是PHP-FPM.

我想知道哪个是更好的两个.在性能和速度方面,哪两个最适合与Nginx一起使用?

BTW,我也在我的服务器上启用了memcached和xcache.

最佳答案
PHP-FPM比旧的FastCGI处理PHP好多了.从PHP 5.3.3 PHP-FPM是核心的,旧的FastCGI实现不再可用了.

我的答案刚刚被投票(在线上一段时间后),我明白为什么,所以这里列出了为什么PHP-FPM实际上比旧的FastCGI实现更好.

首先,众所周知,FastCGI实现在PHP社区中是不好的.一个可以在https://wiki.php.net/ideas/fastcgiwork找到的文件的页面,它说:

php-cgi is not useful in production environment without additional “crutches” (e.g. spawn-fcgi from lighttpd distribution or PHP-fpm patch). This project assumes integration of such “crutches” and extending php-cgi to support for different protocols.

  • daemonization (detach,pid file creation,setup environment variables,setuid/setgid/chroot)
  • graceful restart
  • separate and improve transport layer to allow support for different protocols
  • support for SCGI protocol
  • support for subset of HTTP protocol

这是PHP-FPM从http://php-fpm.org/about/开始做得更好的事情的列表:

  • PHP daemonization: pid file,log file,setsid(),setuid(),setgid(),chroot()
  • Process Management. Ability to “gracefully” stop and start PHP workers without losing any queries. This allows gradually updating the configuration and binary without losing any queries.
  • Restricting IP addresses from which requests can come from.
  • Dynamic number of processes,depending on the load (adaptive process spawning).
  • Starting the workers with different uid/gid/chroot/environment and different PHP.ini options (no need for safe mode).
  • Logging STDOUT and STDERR.
  • Ability to emergency restart all the processes in the event of an accidental destruction of the shared memory opcode cache,if using an accelerator.
  • Forcing the completion of process if set_time_limit() fails.

Additional features:
– Error header
– Accelerated upload support
– 07002
– Slowlog with backtrace

今天关于用golang fastcgi与nginx配合写webgolang实现nginx的讲解已经结束,谢谢您的阅读,如果想了解更多关于cgi fastcgi wsgi isapi fastcgi fastcgi for iis nginx fastcgi pass、django-nginx fastcgi->未处理的异常(从Django 1.2.4升级到Django 1.3之后)、FastCgi与使用Nginx Web服务器的PHP-FPM、FastCgi和PHP-FPM使用Nginx Web服务器的相关知识,请在本站搜索。

本文标签: