dongmin4990 2018-09-16 03:36
浏览 247
已采纳

在端口8000和443上将nginx配置为反向代理,以便在8000上运行的webapp。如何禁用与端口8000的连接?

I configured nginx as a reverse proxy on ports 80 and 443 and had Certbot automatically manage the SSL certificate.

The Golang webapp I have is running on port 8000. Anyone can access the web app through the non-https IP address to the web server with port :8000.

Besides just blocking all traffic to :8000, what's the proper way to disable the port so that the web server is just serving traffic to 80 and 443?

OK, I had my Go app just serve 127.0.0.1:8000 instead of :8000 and now when I navigate to the IP address of my server, I get a 404 not found page. I don't want the IP address of my server served at all. How do I configure that in nginx?

Also, the nginx docs say to "proxy everything" using @proxy: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#proxy-everything (read about it here). Is my config the correct way to do it?

Here is my nginx config for /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;
}

Here is the nginx config for /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;
   }

   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


}
  • 写回答

1条回答 默认 最新

  • dongshi7350 2018-09-16 18:35
    关注

    You can code your application so that it simply does not answer to every address. Rather it only needs to answer to requests from the local host.

    For example:

        log.Fatal(http.ListenAndServe("127.0.0.1:8000", router))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器