1. Khởi chạy
1.1. Chuẩn bị Image
1 2 3 4 5 6 |
// Bản alpine không có "bash" để sử dụng docker pull nginx:1.22-alpine docker pull nginx:1.22 docker run -d -p 8081:80 --name test-nginx nginx:1.22-alpine // Truy cập: localhost://8081 sẽ thấy nginx hoạt động |
1.2. Lấy file Config 😀
1 2 3 4 5 6 7 8 9 10 11 |
// Lấy file config trên Ubuntu hoặc Windows docker run --rm -v /home/duy.ngo/Project/medihome-vn/mh-nginx/:/home/ nginx:1.22 cp /etc/nginx/conf.d/default.conf /home/ docker run --rm -v D:\Project\medihome-vn\mh-nginx\:/home/ nginx:1.22 cp /etc/nginx/conf.d/default.conf /home/ // Phân quyền để được sửa file nginx cd /home/duy.ngo/Project/medihome-vn/ ls -la sudo chmod -R 777 mh-nginx // Tạo docker với config để bên ngoài docker run -d -p 8080:80 -v /home/duy.ngo/Project/medihome-vn/mh-nginx/default.conf:/etc/nginx/conf.d/default.conf --name my-nginx nginx:1.22 |
1.3. Config thêm đường dẫn ./about
Sửa file medihome-vn/mh-nginx/default.conf như sau
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /about { add_header Content-Type text/html; return 200 '<h2>This is About Page</body></h2>'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } |
- Kiểm tra sửa thành công chưa, (-t check cú pháp)
1 2 3 |
docker exec my-nginx cat /etc/nginx/conf.d/default.conf docker exec my-nginx nginx -t docker exec my-nginx nginx -s reload |
- Test thử vào: http://localhost:8080 và http://localhost:8080/about