我有一个本地开发环境,安装了以下内容:
在 nginx.conf 中,我包含了/etc/nginx/conf.d/*.conf,以便成功地将配置文件彼此分开。我还有只有一个配置文件,其中包含从“phpmyadmin”到开发wordpress网站的所有localhost网站的配置,我的目标是正确地将它们作为目录。我确信我的nginx配置很差,可能我应该为url参数定义一个重写规则,但我找不到解决方案:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost-access_log main;
error_log /var/log/nginx/localhost-error_log info;
root /var/www/localhost/htdocs/;
index index.php index.html index.htm;
location ~ \.php$ {
# Test for non-existent scripts or throw a 404 error
# Without this line, nginx will blindly send any request ending in .php to php-fpm
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:run/php-fpm.socket;
}
location /phpmyadmin {
alias /var/www/localhost/htdocs/phpmyadmin;
access_log /var/log/nginx/phpmyadmin-access_log main;
error_log /var/log/nginx/phpmyadmin-error_log info;
}
location /samplewordpress {
alias /var/www/localhost/htdocs/samplewordpress;
access_log /var/log/nginx/samplewordpress-access_log main;
error_log /var/log/nginx/samplewordpress-error_log info;
}
location /szpetra {
alias /var/www/localhost/htdocs/szpetra;
access_log /var/log/nginx/szpetra-access_log main;
error_log /var/log/nginx/szpetra-error_log info;
}
}
我可以登录管理页面,一切正常,但在我尝试加载页面或帖子后,它会返回“拒绝访问”消息。
在将网站保存在目录中时,有什么想法可以解决这个问题吗?
答案 0 :(得分:0)
感谢您的回答@ r3wt。但是,我猜上面的配置文件是不合适的我不想保持原样(但是对于我来说开发似乎没问题,因为部署我宁愿为每个站点使用单独的配置文件)。
因此,别名部分的问题和我应该正确定义 try_files ,因为我想要捕获链接从 localhost / szpetra 所以我添加了它并取消注释别名部分因为现在我清楚地了解 root 和别名之间的区别是:
这将导致在/ foo / bar / bar中搜索文件为附加完整URI 。
location / bar {
root / foo / bar;
}
这将导致在/ foo / bar中搜索文件为仅追加/ bar后的URI部分。
location / bar {
alias / foo / bar;
}
来源:https://forum.nginx.org/read.php?2,129656,130107
现在修改后的配置文件如下所示:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost-access_log main;
error_log /var/log/nginx/localhost-error_log info;
root /var/www/localhost/htdocs/;
index index.php index.html index.htm; # I used index definition here so I think I shouldn't define it under a 'location' field.
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:run/php-fpm.socket;
}
location /phpmyadmin {
alias /var/www/localhost/htdocs/phpmyadmin;
access_log /var/log/nginx/phpmyadmin-access_log main;
error_log /var/log/nginx/phpmyadmin-error_log info;
}
location /samplewordpress {
alias /var/www/localhost/htdocs/samplewordpress;
access_log /var/log/nginx/samplewordpress-access_log main;
error_log /var/log/nginx/samplewordpress-error_log info;
}
location /szpetra {
# alias /var/www/localhost/htdocs/szpetra; #uncommenting alias helped me solve the problem
access_log /var/log/nginx/szpetra-access_log main;
error_log /var/log/nginx/szpetra-error_log info;
# index index.php index.html index.htm; # this doesn't need cause I defined the indexes above in the server{} section but I'm not sure that about this
try_files $uri $uri/ /szpetra/index.php?$args; # Adding this line helped me solve the problem
}
}