dongsu3138 2016-04-28 12:51
浏览 301
已采纳

Nginx + Wordpress页面或帖子URL返回拒绝访问

I have a local development environment with the followings installed:

  • Nginx 1.8.1
  • Wordpress 4.5.1
  • PHP 5.6.20

In nginx.conf I included the /etc/nginx/conf.d/*.conf to successfully separate config files from each other. I have yet only one config file which contains configurations to all of my localhost websites from "phpmyadmin" to under development wordpress sites and my goal is to reach them as directories properly. I'm sure that I have problems in my poorly made nginx configuration, probably I should define a rewrite rule for url parameters but I can't find a solution:

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;
       }

}

I can log in to the admin page and everything works fine, but after I try to load a page or a post, it returns me an "Access denied" message.

Any ideas to solve this problem while I can keep the website in directories?

  • 写回答

1条回答 默认 最新

  • duanpu1111 2016-04-28 14:48
    关注

    Thank you for your answer @r3wt . However, I guess that the configuration file above is improper I wan't to keep it as it is (yet for development seems like okay for me, for deploying I'd rather use separated config files for every site).

    So, the problem with the alias part and I should've define try_files properly as I want to catch links starts with localhost/szpetra so I added it and uncommented the alias part cause now I clearly understand what is the difference between root and alias whis is:

    This will result in files being searched for in /foo/bar/bar as the full URI is appended.

    location /bar {

    root /foo/bar;

    }

    This will result in files being searched for in /foo/bar as only the URI part after /bar is appended.

    location /bar {

    alias /foo/bar;

    }

    Source: https://forum.nginx.org/read.php?2,129656,130107

    Now the modified config file looks like this:

    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 
           }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功