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

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

相关推荐 更多相似问题