完成一键启停、service脚本、README.md

This commit is contained in:
wandoubaba 2024-02-22 13:03:29 +08:00
commit 3cbd055f2c
8 changed files with 244 additions and 0 deletions

38
README.md Normal file
View File

@ -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服务。
本项目目录完全可以交给运维人员维护,前后端程序员只专注于各自的业务项目,由运维人员做好代理和转发。

62
conf/conf.d/default.conf Normal file
View File

@ -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;
#}
}

32
conf/nginx.conf Normal file
View File

@ -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;
}

14
docker-compose.yml Normal file
View File

@ -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

27
html/404.html Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 - Not Found</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
color: #343a40;
text-align: center;
padding: 50px;
}
h1 {
font-size: 3em;
}
p {
font-size: 1.2em;
}
</style>
</head>
<body>
<h1>404 - Not Found</h1>
<p>Sorry, the page you are looking for could not be found.</p>
</body>
</html>

27
html/50x.html Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>500 - Internal Server Error</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
color: #343a40;
text-align: center;
padding: 50px;
}
h1 {
font-size: 3em;
}
p {
font-size: 1.2em;
}
</style>
</head>
<body>
<h1>500 - Internal Server Error</h1>
<p>Sorry, there was an internal server error. Please try again later.</p>
</body>
</html>

27
html/index.html Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>欢迎</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
color: #343a40;
text-align: center;
padding: 50px;
}
h1 {
font-size: 3em;
}
p {
font-size: 1.2em;
}
</style>
</head>
<body>
<h1>欢迎</h1>
<p>docker-nginx</p>
</body>
</html>

17
service Executable file
View File

@ -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