Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created July 21, 2020 21:54
Show Gist options
  • Select an option

  • Save nobonobo/b5d469e7773d63bead8db319ecc720c0 to your computer and use it in GitHub Desktop.

Select an option

Save nobonobo/b5d469e7773d63bead8db319ecc720c0 to your computer and use it in GitHub Desktop.
nginx+go-webserverをdocker-compose upで起動するサンプル
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:
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)
}
}
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;
}
}
}
@nobonobo
Copy link
Copy Markdown
Author

nginx.confのlocation内を

            fastcgi_pass   unix:/var/run/servers/go-fcgi.sock;
            include        fastcgi_params;

にしてgoのhttp.Serveをfcgi.Serveに変えるとFCGI連携もできる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment