2018-09-16 03:36
浏览 208


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))
    点赞 打赏 评论

相关推荐 更多相似问题