docker nginx容器 端口配置
今天在服务器上部署了一个docker的nginx容器,宿主机和容器的端口映射是8181:80
nginx配置文件如下
server {
listen 8181;
server_name docker-test.mydocker.com;
set $project_path user-api/public;
set $docker_root /www;
root $docker_root/$project_path;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass php72:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $docker_root/$project_path/$fastcgi_script_name;
include fastcgi_params;
if ($http_origin) {
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Allow-Headers "Authorization, X-XSRF-TOKEN, Content-Type, Origin, Xmen-Token, Giver-Token" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE" always;
}
}
}
访问后一直报错
curl: (56) Recv failure: Connection reset by peer
一顿研究后发现,nginx配置文件 listen应该是80
,而不是8181
。外部访问地址是http://docker-test.mydocker.com:8181
,但是nginx配置文件是在容器内使用,应该使用容器的端口80
,所以把上面的配置改为容器的端口即可
server {
listen 80;
server_name docker-test.mydocker.com;
set $project_path user-api/public;
set $docker_root /www;
root $docker_root/$project_path;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass php72:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $docker_root/$project_path/$fastcgi_script_name;
include fastcgi_params;
if ($http_origin) {
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Allow-Headers "Authorization, X-XSRF-TOKEN, Content-Type, Origin, Xmen-Token, Giver-Token" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE" always;
}
}
}