我将nginx配置为端口80和443上的反向代理,并让Certbot自动管理SSL证书。
我拥有的Golang Web应用程序在端口8000上运行。任何人都可以通过非https IP地址访问具有端口:8000
的Web服务器的Web应用程序。
除了仅阻止到: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来“代理一切”(在此处阅读有关内容)。我的配置是正确的方法吗?
这是我的/etc/nginx/nginx.conf
的Nginx配置:
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;
}
这是/etc/nginx/sites-enabled/default_server.conf
的nginx配置:
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
}
答案 0 :(得分:4)
您可以对您的应用程序进行编码,以使其不会仅回答每个地址。相反,它只需要回答本地主机的请求即可。
例如:
log.Fatal(http.ListenAndServe("127.0.0.1:8000", router))