Khởi tạo container Nginx ban đầu để trích xuất cấu hình:
docker run --name nginx_temp -p 8080:80 -d nginx:1.18.0
Sao chép các tệp cấu hình và tài nguyên từ container ra máy chủ:
mkdir -p /home/admin1/yyx/nginx/{conf,log,html}
docker cp nginx_temp:/etc/nginx/nginx.conf /home/admin1/yyx/nginx/conf/
docker cp nginx_temp:/etc/nginx/conf.d /home/admin1/yyx/nginx/conf/
docker cp nginx_temp:/usr/share/nginx/html /home/admin1/yyx/nginx/
Phân quyền thư mục để đảm bảo khả năng ghi:
chmod -R 777 /home/admin1/yyx/nginx/
Xóa container tạm và khởi chạy lại với mount volume:
docker stop nginx_temp && docker rm nginx_temp
docker run \
--name nginx_prod \
-p 9002:80 \
-v /home/admin1/yyx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/admin1/yyx/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/admin1/yyx/nginx/log:/var/log/nginx \
-v /home/admin1/yyx/nginx/html:/usr/share/nginx/html \
-d nginx:1.18.0
Nội dung tập tin nginx.conf
user nginx;
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;
include /etc/nginx/conf.d/*.conf;
}
Tập tin MIME types (mime.types)
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/json json;
application/pdf pdf;
font/woff woff;
font/woff2 woff2;
image/png png;
image/svg+xml svg;
image/webp webp;
video/mp4 mp4;
video/webm webm;
application/zip zip;
application/octet-stream bin exe dll iso;
}
Cấu hình mặc định trong default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Trang lỗi mẫu (50x.html)
<html>
<head><title>Lỗi hệ thống</title></head>
<body>
<h1>Đã xảy ra lỗi.</h1>
<p>Trang bạn yêu cầu hiện không khả dụng.</p>
<p>Vui lòng thử lại sau.</p>
<em>— nginx</em>
</body>
</html>
Trang chủ mặc định (index.html)
<html>
<head><title>Chào mừng đến với nginx!</title></head>
<body>
<h1>Chào mừng đến với nginx!</h1>
<p>Nếu bạn thấy trang này, nginx đã được cài đặt thành công.</p>
<p>Tham khảo tài liệu tại <a href="http://nginx.org/">nginx.org</a>.</p>
<em>Cảm ơn bạn đã sử dụng nginx.</em>
</body>
</html>