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 
           }
    

    }

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

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)