如何使用nginx将URI(位置路径)传递给代理?

时间:2018-06-06 07:08:23

标签: wordpress nginx

我正在使用WordPress和nginx。

我需要设置2个访问位置:

  
      
  • mydomainname.com/ - nginx将返回静态文件
  •   
  • mydomainname.com/wordpress - nginx将传递给PHP / WordPress
  •   

我有以下nginx配置文件:

    # https://mydomainname.com - Port 443 ===========================================
server {
    listen       127.0.0.1 ssl;
    server_name  mydomainname.com;
    ssl_certificate /usr/local/etc/letsencrypt/live/mydomainname.com/fullchain.pem;
ssl_certificate_key /usr/local/etc/letsencrypt/live/mydomainname.com/privkey.pem;
ssl_trusted_certificate /usr/local/etc/letsencrypt/live/mydomainname.com/fullchain.pem;
    include  /usr/local/nginx/conf/ssl.conf;
    location / {
        root /usr/local/www/mydomainname.com;
    index index.html;
    }


    index index.php;
    location /wordpress  {                           # I also tried "/wordpress/"
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        root           /usr/local/www/mydomainname.com/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

我想我知道这个问题:URI" / wordpress"或" / wordpress"被删除,不会传递给PHP / Wordpress。

我试图找出如何通过" / wordpress"或" / wordpress /" PHP / WordPress的路径。

我尝试过proxy_pass但它不起作用。

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

root语句移动到server块中,并允许所有位置块继承相同的值。此外,WordPress URI应以/wordpress/为前缀。

例如:

root /usr/local/www/mydomainname.com;

location / {
    index index.html;
}

location /wordpress  {
    index index.php;
    try_files $uri $uri/ /wordpress/index.php?$args;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $request_filename;
}