【发布时间】:2019-02-20 08:59:01
【问题描述】:
我将 nginx 配置为端口 80 和 443 上的反向代理,并让 Certbot 自动管理 SSL 证书。
我的 Golang webapp 在 8000 端口上运行。任何人都可以通过非 https 的 IP 地址访问带有端口 :8000 的 web 服务器的 web 应用程序。
除了阻止到:8000 的所有流量之外,禁用端口以使网络服务器只为80 和443 提供流量的正确方法是什么?
好的,我让我的 Go 应用程序只提供 127.0.0.1:8000 而不是 :8000 现在当我导航到我的服务器的 IP 地址时,我得到一个 404 未找到页面。我根本不希望提供我的服务器的 IP 地址。如何在 nginx 中配置它?
另外,nginx 文档使用@proxy 说“代理一切”:https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#proxy-everything(在此处阅读)。我的配置是正确的方法吗?
这是我的 /etc/nginx/nginx.conf 的 nginx 配置:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 20000;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/json;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# Include server blocks.
include /etc/nginx/sites-enabled/*.conf;
}
这是/etc/nginx/sites-enabled/default_server.conf 的 nginx 配置:
upstream default_server {
server 127.0.0.1:8001;
keepalive 12;
}
server {
server_tokens off;
server_name api.example.com;
client_body_buffer_size 32k;
client_header_buffer_size 8k;
large_client_header_buffers 8 64k;
location / {
proxy_pass http://127.0.0.1:8001;
}
location @proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = api.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name api.example.com;
return 404; # managed by Certbot
}
【问题讨论】:
-
不允许端口 8000 流量到服务器。请求将直接发送到您的代理周围的 go 应用程序。流量是如何导向服务器的?
-
@Brettski 如果您访问
api.mysite.com的主站点,它会带我到我需要去的地方,但我仍然可以通过 My.Server.Ip:8000 访问 Web 服务器。 .. -
有些东西仍在“:8000”上监听。旧版本的 Go 服务器还在运行吗? nginx 配置的上游服务器位于 8001,但 Go 服务器正在侦听 8000。这是怎么回事?
-
似乎原始问题已通过对此问题的回答得到解决。如果您有新问题,请打开一个新问题,而不是仅仅编辑这个问题来添加一个新问题。如果新问题是关于 nginx 配置的,请考虑在 ServerFault 而不是 StackOverflow 上提问。
标签: nginx go reverse-proxy