我有一台 Centos 7 服务器。在这个服务器中我有 Nginx。
在此服务器中,我有一个包含我的应用程序的 Docker。
应用程序.yml:
version: '2'
services:
myBrand-app:
image: myBrand
environment:
- _JAVA_OPTIONS=-Xmx512m -Xms256m
- SPRING_PROFILES_ACTIVE=prod,swagger
- SPRING_DATASOURCE_URL=jdbc:postgresql://myBrand-postgresql:5432/myBrand
- SLEEP=10 # gives time for the database to boot before the application
ports:
- 8080:8080
myBrand-postgresql:
extends:
file: postgresql.yml
service: myBrand-postgresql
是否可以通过我的服务器的IP通过Nginx代理访问Docker?
我想使用 Nginx 作为容器的反向代理。
答案1
以下是如何配置 nginx 将流量从互联网直接路由到您自己的服务器运行的本地主机端口。
当你安装 nginx 时,它通常会在该位置安装其默认配置文件
cat /etc/nginx/nginx.conf
在上述文件的底部附近,您应该会看到类似的内容
include /etc/nginx/sites-enabled/default;
}
如果不只是创建上面的默认文件,如下所示
cat /etc/nginx/sites-enabled/default;
其中可以包含
server {
listen 80 ;
server_name example.com, www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent; # mysettings
}
# .................. enduser .................. #
server { # redirect www to normal domain
listen 443 ssl ;
server_name www.example.com;
include /etc/nginx/mysettings/include/ssl;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl ;
include /etc/nginx/mysettings/include/ssl;
server_name example.com;
include /etc/nginx/snippets/nginx_common_location_443;
location / {
# route to enduser
proxy_pass http://127.0.0.1:3000/;
}
include /etc/nginx/mysettings/include/custom_server_include;
}
在上面你看到这部分:
location / {
# route to enduser
proxy_pass http://127.0.0.1:3000/;
}
它定义了一条路由,将来自外部互联网的流量引导到我的服务器的指定主机和端口,在上面的示例中,该主机和端口位于 127.0.0.1:3000 ...在您的情况下,将我的 3000 替换为您的端口 8080 ...所以现在当浏览器转到
https://example.com
该流量被路由到我的主机端口,该端口运行于
http://127.0.0.1:3000/
为了完整起见,我现在向您展示上面配置文件中提到的一些帮助程序设置文件
cat /etc/nginx/myconfig/include/ssl;
看起来像
#
# Based on https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=nginx-1.4.6&openssl=1.0.1f&hsts=yes&profile=modern
ssl_certificate /mydir/nginx/sslcerts/example.com/fullchain.pem;
ssl_certificate_key /mydir/nginx/sslcerts/example.com/privkey.pem;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:5m;
# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
和另一个配置文件在这里
cat /etc/nginx/snippets/nginx_common_location_443;
其中包含
# the following is required for WebSockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
如果您有多个要定义的路线,您可以将它们放入一个文件中
cat /etc/nginx/myconfig/include/custom_server_include;
看起来类似于
if ( $request_method !~ ^(GET|POST|PUT|PATCH|DELETE|OPTIONS)$ ) {
# now send to error
return 404;
}
location ~* \.(php)$ {
# matches any request ending in php
return 403;
}
location /apataki {
proxy_pass http://localhost:28778/;
}
location /hooks/ {
# this is my webhook server
proxy_pass http://localhost:9000/hooks/;
}
# .......
error_page 404 /error_404.html;
location = /error_404.html {
root /cryptdata/var/deploy;
}
error_page 502 /error_502.html;
location = /error_502.html {
root /cryptdata/var/deploy;
}