Web Server简介

目录

Web Server

原型

1
vim HTTPServer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl

use Socket;
use Crap;
use FileHandle;

# (1) use port 8080 by default, unless overridden on command line
$port = (@ARGV ? $ARGV[0] : 8080);

# (2) create local TCP socket and set it to listen for connections
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || die;
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;
bind(S, sockaddr_in($port, INADDR_ANY)) || die;
listen(S, SOMAXCONN) || die;

# (3) print a startup message
printf("<<<Type-O-Serve Accepting on Port %d>>>\n\n", $port);

while(1)
{
# (4) wait for connection C
$cport_caddr = accept(C, S);
($cport, $caddr) = sockaddr_in($cport_caddr);
C->autoflush(1);

# (5) print who the connection is from
$cname = gethostbyaddr($caddr, AF_INET);
printf("Request From '%s'>>>\n", $cname);

# (6) read request msg until blank line, and print on screen
while ($line = <C>)
{
print $line;
if ($line =~ /^\r/) {
last;
}
}

# (7) prompt for response message, and input response lines,
# sending response lines to clinet, until solitary "."
printf("<<<Type Response Followed by '.'>>>\n");

while ($line = <STDIN>)
{
$line =~ s/\r//;
$line =~ s/\n//;
if ($line =~ /^\./) {
last;
}
print C $line . "\r\n";
}
close(C);
}
1
2
3
cpan crap

perl HTTPServer.pl 8000
1
2
3
4
5
6
7
8
9
10
11
<<<Type-O-Serve Accepting on Port 8000>>>

Request From 'localhost'>>>
GET / HTTP/1.1
Host: localhost:8000
User-Agent: curl/7.54.0
Accept: */*

<<<Type Response Followed by '.'>>>
hello world
.
1
2
curl localhost:8000
# hello world

作用

  1. 接受客户端连接

  2. 接收请求报文

  3. 对资源的映射和访问

  4. 构建响应

  5. 发送响应

  6. 记录日志

方案

Web Application

FastCGI

CGI(Common Gateway Interface)即通用网关接口 它是Web Server与Web Application之间数据交换的一种协议

  • HTTP Request的Header设置成进程的环境变量, HTTP Request的正文设置成进程的标准输入

  • 进程的标准输出就是HTTP Response, 包括Header和正文

FastCGI(Fast Common Gateway Interface)即快速通用网关接口 它是CGI的增強版本 通过进程管理等优化方法提升了服务性能

PHP-FPM

PHP-FPM = PHP FastCGI Process Manager

web-server-01.png

Tomcat

Tomcat是一个免费的开放源代码的Servlet容器 它是基于Apache httpd的扩展

web-server-02.png

1
2
3
spring init -a demo -b 2.0.6.RELEASE --build gradle -dweb HTTPSDemo

cd HTTPSDemo
1
vim src/main/java/com/example/demo/DemoApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
  • 测试
1
2
3
4
./gradlew bootrun

curl localhost:8080/hello
# hello world
1
./gradlew dependencies | grep tomcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<-------------> 0% EXECUTING [2s]
> :dependencies > Resolve dependencies of :testCompileClasspath
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| | \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| | \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
<-------------> 0% WAITING
> IDLE
1
2
3
4
5
keytool -genkey -alias tomcat -dname "CN=Andy,OU=kfit,O=kfit,L=HaiDian,ST=BeiJing,C=CN" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 365

mv keystore.p12 src/main/resources

vim src/main/resources/application.properties
1
2
3
4
5
server.port=443
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-alias=tomcat
  • 测试
1
2
3
4
./gradlew bootrun

curl -k https://localhost/hello
# hello world

参考