为了账号安全,请及时绑定邮箱和手机立即绑定

将 nginx 配置为端口 80 和 443 上的反向代理,用于在 8000 上运行的 webapp

将 nginx 配置为端口 80 和 443 上的反向代理,用于在 8000 上运行的 webapp

Go
凤凰求蛊 2023-04-04 15:23:31
我将 nginx 配置为端口 80 和 443 上的反向代理,并让 Certbot 自动管理 SSL 证书。我拥有的 Golang webapp 在端口 8000 上运行。任何人都可以通过非 https IP 地址访问 web 应用程序到具有端口的 web 服务器:8000。除了阻止到 的所有流量之外:8000,禁用端口的正确方法是什么,以便 Web 服务器只为到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(在此处阅读)。我的配置是正确的方法吗?这是我的 nginx 配置/etc/nginx/nginx.conf: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;}这是 nginx 配置/etc/nginx/sites-enabled/default_server.conf: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;   }
查看完整描述

1 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

您可以对您的应用程序进行编码,这样它就不会对每个地址作出答复。相反,它只需要回答来自本地主机的请求。

例如:

    log.Fatal(http.ListenAndServe("127.0.0.1:8000", router))


查看完整回答
反对 回复 2023-04-04
  • 1 回答
  • 0 关注
  • 191 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信