nginx - Nginx 显示欢迎页面/403 错误而不是主页
问题描述
我正在使用 CentOs 6.8
在开发模式下使用 CloudFlare DNS
Nginx 显示欢迎页面,从
/usr/share/nginx/html
但不是来自:
/home/nginx/domains/XYZDomain.com/public/
在目录中:
/etc/nginx/conf.d
2个配置文件:
default.conf
虚拟.conf
default.conf 文件输出
# Main Local
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
}
virtual.conf 文件输出:
server {
listen 80;
#server_name www.XYZDomain.com;
# Now Changed to below withouth wwww.
server_name XYZDomain.com;
rewrite ^/(.*) http://XYZDomain.com/$1 permanent;
location / {
root /home/nginx/domains/XYZDomain.com/public/;
index index.html;
}
}
/etc/nginx/nginx.conf 输出中的 nginx.conf 文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
#default_type application/octet-stream;
#changed to text/html
default_type text/html;
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
所有具有权限的目录。
ls -la /home/nginx/domains/XYZDomain.com
drwxr-s--- 6 root root 4096 Jul 1 12:54 .
drwxr-xr-x 3 root root 4096 Jul 2 14:44 ..
drwxr-s--- 2 root root 4096 Jun 30 15:58 backup
drwxr-s--- 2 root root 4096 Jun 30 15:58 log
drwxr-s--- 2 root root 4096 Jun 30 15:58 private
drwxr-sr-x 2 root root 4096 Jul 2 15:01 public
我试过修改 default.php 和 virtual.conf 文件
任何人都可以帮助我这有什么问题吗?我真的很困惑,为此浪费了一整天。
解决方案
看起来这是一个权限问题。Nginx 以nginx
用户身份运行,但文件归其所有root
且没有全局读取权限,这意味着nginx
用户无法看到它们。
首先,不要以 root 身份运行 ngninx!这真是太糟了。如果有人破坏了您的站点,他们可能具有对服务器的 root 访问权限。
有几种方法可以解决这个问题。最简单的方法是将文件的所有者更改为nginx
:
chown -R nginx:nginx /home/nginx/domains/XYZDomain.com
当然,您必须记住对您创建的任何新文件执行此操作
您还可以仅将文件的组更改为nginx
,并使它们组可读:
chgrp -R nginx /home/nginx/domains/XYZDomain.com
chmod -R g+r /home/nginx/domains/XYZDomain.com
第二种方式更安全一点,因为 nginx 没有写权限,只能读取文件。但是,如果您有需要动态创建或编辑文件的脚本,这可能会导致其他问题。
关于权限和 Web 服务器还有很多内容需要了解,其中涉及很多安全隐患,这里就不多说了。如果您好奇,可以在 stackoverflow.com 上找到大量信息。
推荐阅读
- c# - 在 wpf 后面创建控件异步代码
- linux - 从 Windows 命令提示符 ssh 时主机密钥验证失败?
- c# - 我应该使用什么命名空间在 .net 中使用 Row 方法
- scala - Spark函数不可序列化
- javascript - 节点应用程序在本地主机上工作但在 Heroku 上不工作?
- selenium - 如何在 Selenium IDE 中处理多个 URL?
- python - 如何使用 opencv 显示实时视频源?
- excel - VBA Excel:AdvancedFilter 以在 AutoFilter 之后获取唯一值
- http - 将 HTTP 请求转换为 MQTT 并再次转换为智能家居集成
- opengl - 如何表示将存储在线程本地存储中的 EGL 类型的所有权和生命周期?