From 3cbd055f2c8274fa5d0a4b2bb93b70f5cfcb13e7 Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Thu, 22 Feb 2024 13:03:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=80=E9=94=AE=E5=90=AF?= =?UTF-8?q?=E5=81=9C=E3=80=81service=E8=84=9A=E6=9C=AC=E3=80=81README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 38 ++++++++++++++++++++++++ conf/conf.d/default.conf | 62 ++++++++++++++++++++++++++++++++++++++++ conf/nginx.conf | 32 +++++++++++++++++++++ docker-compose.yml | 14 +++++++++ html/404.html | 27 +++++++++++++++++ html/50x.html | 27 +++++++++++++++++ html/index.html | 27 +++++++++++++++++ service | 17 +++++++++++ 8 files changed, 244 insertions(+) create mode 100644 README.md create mode 100644 conf/conf.d/default.conf create mode 100644 conf/nginx.conf create mode 100644 docker-compose.yml create mode 100644 html/404.html create mode 100644 html/50x.html create mode 100644 html/index.html create mode 100755 service diff --git a/README.md b/README.md new file mode 100644 index 0000000..2cc8318 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# docker-nginx + +基于docker实现的nginx服务 + +## 使用方法 + +```sh +git clone https://git.wandoubaba.com/wandoubaba/docker-nginx.git +cd docker-nginx +docker compose up -d +``` + +服务启停方法 + +```sh +# 停止服务 +./service stop +# 启动服务 +./service start +# 重启服务(service脚本中的容器名称要与docker-compose.yml文件中的容器名称对应) +./service restart +# 重载配置 +./service reload +# 服务状态 +./service status +``` + +## 配置 + +- 在`docker-compose.yml`中可以修改容器名称。 +- `conf/conf.d/default.conf`文件是默认站点的配置文件。 +- 在`conf/conf.d/`中可以创建多个站点的配置文件,注意相互之间的域名和端口号不要同时冲突。 + +## 最佳实践 + +建议不要在本项目的目录中加入任何业务文件,本项目做为单独目录,只提供nginx服务。 + +本项目目录完全可以交给运维人员维护,前后端程序员只专注于各自的业务项目,由运维人员做好代理和转发。 diff --git a/conf/conf.d/default.conf b/conf/conf.d/default.conf new file mode 100644 index 0000000..eee3aae --- /dev/null +++ b/conf/conf.d/default.conf @@ -0,0 +1,62 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + #access_log /var/log/nginx/host.access.log main; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + # 下面这行可以解决vue网页刷新后404的问题,但会将所有不存在的url转到index.html上 + # try_files $uri $uri/ /index.html last; + } + + # 反向代理示例 + # 将/api/的请求转到127.0.0.1:8081 + # + # location /api/ { + # proxy_next_upstream http_502 http_504 error timeout invalid_header; + # proxy_pass http://127.0.0.1:8081/; + # 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_set_header X-Forwarded-Proto $scheme; + # } + + error_page 404 /404.html; + location = /404.html { + root /usr/share/nginx/html; + } + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +} + diff --git a/conf/nginx.conf b/conf/nginx.conf new file mode 100644 index 0000000..5e076aa --- /dev/null +++ b/conf/nginx.conf @@ -0,0 +1,32 @@ + +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +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; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + include /etc/nginx/conf.d/*.conf; +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9d3f1c8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: "3.1" + +services: + nginx: + image: nginx:1.25 + container_name: nginx + restart: always + volumes: + - ./conf/nginx.conf:/etc/nginx/nginx.conf + - ./conf/conf.d:/etc/nginx/conf.d + - ./html:/usr/share/nginx/html + working_dir: /usr/share/nginx/html + stdin_open: true + network_mode: host \ No newline at end of file diff --git a/html/404.html b/html/404.html new file mode 100644 index 0000000..567cfec --- /dev/null +++ b/html/404.html @@ -0,0 +1,27 @@ + + + + + +404 - Not Found + + + +

404 - Not Found

+

Sorry, the page you are looking for could not be found.

+ + diff --git a/html/50x.html b/html/50x.html new file mode 100644 index 0000000..2ade389 --- /dev/null +++ b/html/50x.html @@ -0,0 +1,27 @@ + + + + + +500 - Internal Server Error + + + +

500 - Internal Server Error

+

Sorry, there was an internal server error. Please try again later.

+ + diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..cc7b27a --- /dev/null +++ b/html/index.html @@ -0,0 +1,27 @@ + + + + + +欢迎 + + + +

欢迎

+

docker-nginx

+ + diff --git a/service b/service new file mode 100755 index 0000000..c60d819 --- /dev/null +++ b/service @@ -0,0 +1,17 @@ +#!/bin/bash + +container_name="nginx" + +if [ "$1" = "start" ]; then + docker compose up -d +elif [ "$1" = "stop" ]; then + docker compose down +elif [ "$1" = "restart" ]; then + docker restart $container_name +elif [ "$1" = "reload" ]; then + docker exec -i $container_name service nginx reload +elif [ "$1" = "status" ]; then + docker exec -i $container_name service nginx status +else + echo "Usage: $0 [start|stop|restart|reload|status]" +fi