https代理到localhost之后的nginx超时
我想运行一个docker -nook,它只是其他docker-compose服务的代理。
这是我的docker-compose.yml与代理:
version: '2' services: storage: image: nginx:1.11.13 entrypoint: /bin/true volumes: - ./config/nginx/conf.d:/etc/nginx/conf.d - /path_to_ssl_cert:/path_to_ssl_cert proxy: image: nginx:1.11.13 ports: - "80:80" - "443:443" volumes_from: - storage network_mode: "host"
所以它会把所有的连接都连接到端口80或443,并将它们代理到./config/nginx/conf.d
目录中指定的服务。
这里是示例服务./config/nginx/conf.d/domain_name.conf
:
server { listen 80; listen 443 ssl; server_name domain_name.com; ssl_certificate /path_to_ssl_cert/cert; ssl_certificate_key /path_to_ssl_cert/privkey; return 301 https://www.domain_name.com$request_uri; } server { listen 80; server_name www.domain_name.com; return 301 https://www.domain_name.com$request_uri; # If you uncomment this section and comment return line it's works # location ~ { # proxy_pass http://localhost:8888; # # or proxy to https, doesn't matter # #proxy_pass https://localhost:4433; # } } server { listen 443 ssl; server_name www.domain_name.com; ssl on; ssl_certificate /path_to_ssl_cert/cert; ssl_certificate_key /path_to_ssl_cert/privkey; location ~ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Client-Verify SUCCESS; proxy_set_header X-Client-DN $ssl_client_s_dn; proxy_set_header X-SSL-Subject $ssl_client_s_dn; proxy_set_header X-SSL-Issuer $ssl_client_i_dn; proxy_pass https://localhost:4433; # like before # proxy_pass http://localhost:8888; } }
它将所有请求http://domain_name.com
: https://domain_name.com
和http://www.domain_name.com
redirect到https://www.domain_name.com
并将其代理到特定的本地主机服务。
这里是我的特定服务docker-compose.yml
version: '2' services: storage: image: nginx:1.11.13 entrypoint: /bin/true volumes: - /path_to_ssl_cert:/path_to_ssl_cert - ./config/nginx/conf.d:/etc/nginx/conf.d - ./config/php:/usr/local/etc/php - ./config/php-fpm.d:/usr/local/etc/php-fpm.d - php-socket:/var/share/php-socket www: build: context: . dockerfile: ./Dockerfile_www image: domain_name_www ports: - "8888:80" - "4433:443" volumes_from: - storage links: - php php: build: context: . dockerfile: ./Dockerfile_php image: domain_name_php volumes_from: - storage volumes: php-socket:
所以当你去http://www.domain_name.com:8888
或https://www.domain_name.com:4433
你会得到内容。 当你从docker运行的服务器上curl到localhost:8888
或https://localhost:4433
,你也会得到内容。
现在我的问题。
当我去浏览器并键入domain_name.com
, www.domain_name.com
或https://www.domain_name.com
什么也没有发生。 即使当我从本地机器curl到这个域时,我也会超时。
我有search一些信息“nginx代理https到本地”,但注意到我的作品。
我有解决scheme!
当我在docker-compose.yml
为我的代理设置network_mode: "host"
docker-compose.yml
,我认为ports:
entries仍然在工作,但是没有。
现在,代理在我的主机networking中工作,因此它使用本地端口并省略ports:
来自docker-compose.yml
条目。 这意味着我已经手动打开我的服务器上的端口80和443。