如何解决如何在nginx上制作2台服务器一个可以通过IP访问,另一个可以通过localhost访问
一个是/ var / www / html,我希望可以通过我的IP访问:80,另一个是denny
一个是/ var / www / cats,我希望我的本地主机可以访问它:80,另一个是denny
如何在Nginx设置中找到它?
UPD。 1
我有2个配置
sudo nano /etc/Nginx/sites-enabled/default
server {
listen 122.111.111.40:80;
root /var/www/html/;
index /;
server_name 122.111.111.40;
location / {
allow 122.111.111.40;
deny all;
autoindex on;
index index.PHP;
try_files $uri /index.html /index.PHP;
}
location ~ \.PHP$ {
include snippets/fastcgi-PHP.conf;
fastcgi_pass unix:/run/PHP/PHP7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo nano /etc/Nginx/sites-enabled/cats
server {
listen 127.0.0.1:80;
root /var/www/cats/;
index /index.PHP;
server_name localhost;
location / {
allow 127.0.0.1;
deny all;
autoindex on;
index index.PHP;
try_files $uri /index.html /index.PHP;
}
location ~ \.PHP$ {
include snippets/fastcgi-PHP.conf;
fastcgi_pass unix:/run/PHP/PHP7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
虽然我可以访问本地主机,但可以。我的cats/index.PHP
文件夹路由正常。在路由到我的IP时,我有403 Forbidden
2020/09/22 05:44:02 [error] 17563#17563: *853 access forbidden by rule,client: 115.111.11.111,server: localhost,request: "GET / HTTP/1.1",host: "122.111.111.40".
所以我的配置有些问题。但是我不明白是哪一个。如果我请求我的IP/index.PHP
,我会看到一个索引页面cats/index.PHP
,但是该页面应该是html/index.PHP
。对吧?
UPD。 2
添加sudo nano /etc/Nginx/snippets/fastcgi-PHP.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.Nginx.org/Nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.PHP;
include fastcgi.conf;
UPD。 3
添加sudo nano /etc/Nginx/fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE Nginx/$Nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only,required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
解决方法
假设您没有任何其他站点,则以下配置应该有效:
server {
listen <ip>:80;
root /var/www/html;
...
}
server {
listen 127.0.0.1:80;
root /var/www/cats;
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。