使用docker安装nginx后会生成两个配置文件分别是/etc/nginx/nginx.conf和/etc/nginx/conf.d/default.conf
此时如果需要配置反向代理,需要修改default.conf文件。
修改方式一:
安装vim编辑器后直接在容器内修改配置文件并退出重启容器
安装vim编辑器请参考本链接: docker进入容器修改配置文件.
修改方式二:
将上面两个文件在启动容器命令中添加文件挂载
如: docker run -itd -p 8800:80 --name test-nginx -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf nginx
default.conf文件内容:
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /{
root /usr/share/nginx/html;
index index.html index.htm;
}
location /test {
proxy_pass http://myserver/test;
}
#error_page 404 /404.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;
#}
}
## 容器内网ip和nginx在同一网络,未配置端口映射,用nginx实现反向代理+负载均衡。
upstream myserver {
server 172.18.0.6:8080;
server 172.18.0.3:8080;
}
nginx.conf
user nginx;
worker_processes 1;
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}