一
首先下載nginx,
下載頁面
作者用的是windows谨读,所以我這里就下window的1.13.12版本局装。
下載完成后解壓到C盤:
nginx
打開我們的命令行窗口,定位到我們的nginx目錄劳殖,
start nginx
start nginx
我們可以打開瀏覽器訪問以下http://localhost
nginx 安裝成功
如果你能看到上圖铐尚,就說明安裝成功了。
且放一邊哆姻。宣增。。
二
下載Go
同樣的矛缨,還是選擇window版本爹脾,下載完成后,一路next箕昭,就默認(rèn)裝到C盤灵妨。
三
下載Goland
激活碼搜一下http://idea.youbbs.org
New Project
命名為GoTest
依次新建 src
目錄,新建http1
目錄落竹,并新建一個(gè)http1.go
GoTest項(xiàng)目結(jié)構(gòu)
http1.go:
package main
import (
"fmt"
"net/http"
"strings"
"log"
)
func printRequest(r *http.Request) {
fmt.Println("r.Form=", r.Form) //這些信息是輸出到服務(wù)器端的打印信息 , Get參數(shù)
fmt.Println("r.PostForm=", r.PostForm) //Post參數(shù)
fmt.Println("path=", r.URL.Path)
fmt.Println("scheme=", r.URL.Scheme)
fmt.Println("method=", r.Method) //獲取請(qǐng)求的方法
fmt.Println("Http Get參數(shù)列表 begin:")
for k, v := range r.Form {
fmt.Println("Http Get["+k+"]=", strings.Join(v, " ; "))
}
fmt.Println("Http Get參數(shù)列表 end:")
fmt.Println("Http Post參數(shù)列表 begin:")
for k, v := range r.PostForm {
fmt.Println("Http Post["+k+"]=", strings.Join(v, " ; "))
}
fmt.Println("Http Post參數(shù)列表 end:")
arraA := r.Form["a"]
fmt.Println("r.Form['a']=", arraA)
if len(arraA) > 0 {
fmt.Println("r.Form['a'][0]=", arraA[0])
}
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析參數(shù)闷串,默認(rèn)是不會(huì)解析的
fmt.Println("******************************************sayhelloName")
printRequest(r)
fmt.Fprintf(w, "<h1>Hello 世界!</h1>") //這個(gè)寫入到w的是輸出到客戶端的
}
func sayMore(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析參數(shù),默認(rèn)是不會(huì)解析的
fmt.Println("******************************************sayMore")
printRequest(r)
fmt.Fprintf(w, "<h1>Hello More!</h1>") //這個(gè)寫入到w的是輸出到客戶端的
}
func sayMore1(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析參數(shù)筋量,默認(rèn)是不會(huì)解析的
fmt.Println("******************************************sayMore1")
printRequest(r)
fmt.Fprintf(w, "<h1>Hello More1!</h1>") //這個(gè)寫入到w的是輸出到客戶端的
}
func main() {
http.HandleFunc("/", sayhelloName) //設(shè)置訪問的路徑
http.HandleFunc("/more", sayMore) //設(shè)置訪問的路徑
http.HandleFunc("/more/", sayMore1) //設(shè)置訪問的路徑
err := http.ListenAndServe(":9090", nil) //設(shè)置監(jiān)聽的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
運(yùn)行http1.go
運(yùn)行http1.go
運(yùn)行之后,我們打開瀏覽器訪問http://localhost:9090
訪問結(jié)果
在goland中碉熄,我們也能看到打印的日志:
訪問日志
四
設(shè)置nginx反向代理
打開
nginx.conf
修改之后的文件
nginx.conf:
#運(yùn)行用戶
#user nobody;
#啟動(dòng)進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等
worker_processes 1;
#全局錯(cuò)誤日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及連接數(shù)上限
events {
#epoll是多路復(fù)用IO(I/O Multiplexing)中的一種方式,
#僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能
#use epoll;
#單個(gè)后臺(tái)worker process進(jìn)程的最大并發(fā)鏈接數(shù)
worker_connections 1024;
# 并發(fā)總數(shù)是 worker_processes 和 worker_connections 的乘積
# 即 max_clients = worker_processes * worker_connections
# 在設(shè)置了反向代理的情況下桨武,max_clients = worker_processes * worker_connections / 4 為什么
# 為什么上面反向代理要除以4,應(yīng)該說是一個(gè)經(jīng)驗(yàn)值
# 根據(jù)以上條件锈津,正常情況下的Nginx Server可以應(yīng)付的最大連接數(shù)為:4 * 8000 = 32000
# worker_connections 值的設(shè)置跟物理內(nèi)存大小有關(guān)
# 因?yàn)椴l(fā)受IO約束呀酸,max_clients的值須小于系統(tǒng)可以打開的最大文件數(shù)
# 而系統(tǒng)可以打開的最大文件數(shù)和內(nèi)存大小成正比,一般1GB內(nèi)存的機(jī)器上可以打開的文件數(shù)大約是10萬左右
# 我們來看看360M內(nèi)存的VPS可以打開的文件句柄數(shù)是多少:
# $ cat /proc/sys/fs/file-max
# 輸出 34336
# 32000 < 34336琼梆,即并發(fā)連接總數(shù)小于系統(tǒng)可以打開的文件句柄總數(shù)性誉,這樣就在操作系統(tǒng)可以承受的范圍之內(nèi)
# 所以窿吩,worker_connections 的值需根據(jù) worker_processes 進(jìn)程數(shù)目和系統(tǒng)可以打開的最大文件總數(shù)進(jìn)行適當(dāng)?shù)剡M(jìn)行設(shè)置
# 使得并發(fā)總數(shù)小于操作系統(tǒng)可以打開的最大文件數(shù)目
# 其實(shí)質(zhì)也就是根據(jù)主機(jī)的物理CPU和內(nèi)存進(jìn)行配置
# 當(dāng)然,理論上的并發(fā)總數(shù)可能會(huì)和實(shí)際有所偏差错览,因?yàn)橹鳈C(jī)還有其他的工作進(jìn)程需要消耗系統(tǒng)資源纫雁。
# ulimit -SHn 65535
}
http {
#設(shè)定mime類型,類型由mime.type文件定義
include mime.types;
default_type application/octet-stream;
#設(shè)定日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,
#對(duì)于普通應(yīng)用倾哺,必須設(shè)為 on,
#如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用轧邪,可設(shè)置為 off,
#以平衡磁盤與網(wǎng)絡(luò)I/O處理速度羞海,降低系統(tǒng)的uptime.
sendfile on;
#tcp_nopush on;
#連接超時(shí)時(shí)間
#keepalive_timeout 0;
keepalive_timeout 65;
#tcp_nodelay on;
#開啟gzip壓縮
#gzip on;
#gzip_disable "MSIE [1-6].";
#設(shè)定請(qǐng)求緩沖
#client_header_buffer_size 128k;
#large_client_header_buffers 4 128k;
#設(shè)定虛擬主機(jī)配置
server {
#偵聽80端口
listen 80;
#定義使用 www.nginx.cn訪問
server_name www.nginx.cn;
#定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置
root html;
#設(shè)定本虛擬主機(jī)的訪問日志
#access_log logs/nginx.access.log main;
#默認(rèn)請(qǐng)求
location / {
#定義首頁索引文件的名稱
index index.php index.html index.htm;
}
# 定義錯(cuò)誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#靜態(tài)文件忌愚,nginx自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#過期30天,靜態(tài)文件不怎么更新却邓,過期可以設(shè)大一點(diǎn)硕糊,
#如果頻繁更新,則可以設(shè)置得小一點(diǎn)腊徙。
expires 30d;
}
#PHP 腳本請(qǐng)求全部轉(zhuǎn)發(fā)到 FastCGI處理. 使用FastCGI默認(rèn)配置.
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#禁止訪問 .htxxx 文件
location ~ /.ht {
deny all;
}
}
## 設(shè)置反向代理简十,指向go服務(wù)器 ##
server {
listen 8081;
#server_name www.xxx.cn;
#access_log logs/quancha.access.log main;
#error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;
## send request back to apache ##
location / {
proxy_pass http://localhost:9090;#go 服務(wù)器可以指定到其他的機(jī)器上,這里設(shè)定本機(jī)服務(wù)器
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#proxy_max_temp_file_size 0;
#proxy_connect_timeout 90;
#proxy_send_timeout 90;
#proxy_read_timeout 90;
#proxy_buffer_size 4k;
#proxy_buffers 4 32k;
#proxy_busy_buffers_size 64k;
#proxy_temp_file_write_size 64k;
}
}
## End 設(shè)置反向代理昧穿,指向go服務(wù)器 ##
}
五
打開我們的最早的命令行窗口,輸入nginx -s reload
重載nginx配置
訪問http://localhost:8081/
訪問結(jié)果
OK成功訪問到我們nginx配置的go服務(wù)器勺远。
我們還可以使用Post的方式帶上路徑或者參數(shù)訪問
比如:
postman 請(qǐng)求
打印日志:
goland 打印輸出
上面那個(gè)日志可以看出r.Form
包含了r.PostForm
參數(shù)。