一、Docker创建Nginx容器
Docker命令
docker run -d\--name nginx \-p80:80 \-v /root/nginx/dist:/usr/share/nginx/html \-v /root/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/json;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /root/nginx/dist;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root/nginx/dist;
}
}
}
二、403 Forbidden
2.1 问题分析
默认情况下,使用Docker创建的Nginx容器默认会以
nginx
用户运行,而不是
root
用户
2.2 解决方案
在Nginx配置文件中,指定Nginx以
root
用户运行
user root; # 此配置应添加到Nginx配置文件的开头
user root; # 此配置应添加到Nginx配置文件的开头
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/json;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /root/nginx/dist;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root/nginx/dist;
}
}
}
三、404 Not Found
3.1 问题分析
Nginx配置文件中
location /
块的
root
指令,错误地指向了主机上的
/root/nginx/dist
目录,而不是容器内的
/usr/share/nginx/html
目录
3.2 解决方案
修改Nginx配置文件中
location /
块的
root
指令,确保其指向容器内的
/usr/share/nginx/html
目录
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 404 500 502 503 504 /index.html;
location = /index.html {
root /usr/share/nginx/html;
}
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/json;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 404 500 502 503 504 /index.html;
location = /index.html {
root /usr/share/nginx/html;
}
}
}
版权归原作者 枯木何日可逢春 所有, 如有侵权,请联系我们删除。