我已经使用 uwsgi 设置了一个 nginx Web 服务器来托管我的 flask 应用程序。当我通过 LAN ip(192.168.1.x)访问它时,我可以正常访问该网站,但是当我通过我的公共 IP 访问它时,我收到“404 Not Found nginx/1.14.0 (Ubuntu)”的提示。这是为什么?我该如何修复它?
/etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
可用站点
server {
listen 80;
server_name 192.168.1.108;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
项目文件
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = true
答案1
您特别要求您的网站仅回答 RFC1918 IP:
server_name 192.168.1.108;
您的全局 IP 地址不是 192.168.1.108,因此它永远不会匹配此server
块,因此将由默认块提供服务server
。
为了解决这个问题,请购买一个域名,为域中的名称设置一个地址记录,然后将设置server_name
为该名称。
答案2
这里有 3 种可能性 - 取决于您以后的需求
选项一:
/etc/nginx/sites-enabled/optionone
server {
listen 80;
server_name 192.168.1.108 1.2.3.4;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
一体化,易于阅读
选项二
/etc/nginx/sites-avaible/optiontwo
server {
listen 80;
server_name 192.168.1.108;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
server {
listen 80;
server_name 1.2.3.4;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
也易于阅读,但将其分成两个单独的实例,方便调试
和
选项三
/etc/nginx/sites-enabled/选项-three-int
server {
listen 80;
server_name 192.168.1.108;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
/etc/nginx/sites-enabled/option-three-pub
server {
listen 80;
server_name 1.2.3.4;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/matthew/myproject/myproject.sock;
}
}
每个站点都有自己的文件,便于以后处理,例如用于 certbot,并且以后非常容易阅读并知道哪个文件做什么。
正如你所看到的,这可以通过多种方式来实现 - 每种方式都有其“该做的和不该做的事”
提醒:
不要忘记将网站激活(链接)到 sites-enabled