Created
July 21, 2020 21:54
-
-
Save nobonobo/b5d469e7773d63bead8db319ecc720c0 to your computer and use it in GitHub Desktop.
nginx+go-webserverをdocker-compose upで起動するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version: "3" | |
| services: | |
| nginx: | |
| image: nginx:alpine | |
| ports: | |
| - "80:80" | |
| volumes: | |
| - ./nginx.conf:/etc/nginx/nginx.conf | |
| - sockets:/var/run/servers | |
| server: | |
| image: golang:alpine | |
| working_dir: /app | |
| command: go run . | |
| volumes: | |
| - ./:/app | |
| - sockets:/var/run/servers | |
| volumes: | |
| sockets: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net" | |
| "net/http" | |
| "os" | |
| ) | |
| func index(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, "hello world!!") | |
| } | |
| func main() { | |
| http.HandleFunc("/", index) | |
| os.Remove("/var/run/servers/go-http.sock") | |
| l, err := net.Listen("unix", "/var/run/servers/go-http.sock") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| if err := http.Serve(l, nil); err != nil { | |
| log.Println(err) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| user root; | |
| worker_processes auto; | |
| error_log /var/log/nginx/error.log warn; | |
| pid /var/run/nginx.pid; | |
| events { | |
| worker_connections 1024; | |
| } | |
| http { | |
| include /etc/nginx/mime.types; | |
| default_type application/octet-stream; | |
| 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 /var/log/nginx/access.log main; | |
| sendfile on; | |
| keepalive_timeout 65; | |
| upstream nginx-internal-sock { | |
| server unix:/var/run/servers/go-http.sock; | |
| } | |
| server { | |
| listen 80; | |
| server_name localhost; | |
| error_page 500 502 503 504 /50x.html; | |
| location = /50x.html { | |
| root /usr/share/nginx/html; | |
| } | |
| location / { | |
| proxy_set_header Host $host; | |
| proxy_set_header X-Real-IP $remote_addr; | |
| proxy_set_header Accept-Encoding ""; | |
| proxy_pass http://nginx-internal-sock; | |
| } | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nginx.confのlocation内を
にしてgoのhttp.Serveをfcgi.Serveに変えるとFCGI連携もできる。