dongmin4990 2018-09-16 03:36
浏览 247


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 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: (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/;

# 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
    # 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 {
    keepalive 12;

server {
    server_tokens off;

    client_body_buffer_size     32k;
    client_header_buffer_size   8k;
    large_client_header_buffers 8 64k;
    location / {

   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/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # 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 = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 default_server;
    listen [::]:80 default_server;
    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("", router))
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对