使用NGINX为郊区服务PHP网站

时间:2017-06-17 07:04:29

标签: php nginx

有以下nginx配置:

server {
        listen 80;
        listen [::]:80;

        location ~* \.(png|jpg|jpeg|gif|ico)$ { expires 2d; add_header Cache-Control "public, no-transform"; }

        server_name myurl.com;
        client_max_body_size 32m;
        root /home/user/www/wordpress;
        index index.html index.php;

        location ~* \.(css|js)$ {
          gzip_static on;
        }

        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              include fastcgi_params;
              fastcgi_pass unix:/run/php/php7.0-fpm.sock;
              fastcgi_param SCRIPT_FILENAME /home/user/www/wordpress$fastcgi_script_name;
              fastcgi_param PHP_VALUE post_max_size=20M;
              fastcgi_param PHP_VALUE upload_max_filesize=20M;
        }
}

它很好用;如果我访问myurl.com,我可以看到我的网站。所以,现在我希望我的网站将在myurl.com/suburl中,而不是根网址。我该怎么做?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我猜这是一个WordPress博客。解决方案是使用alias directive指向WordPress安装并使用nested location blocks

例如:

server {
    listen 80;
    listen [::]:80;

    server_name myurl.com;
    client_max_body_size 32m;

    location ^~ /suburl {
        alias /home/user/www/wordpress;
        index index.php;

        if (!-e $request_filename) { rewrite ^ /suburl/index.php last; }

        location ~* \.(png|jpg|jpeg|gif|ico)$ { 
            expires 2d; 
            add_header Cache-Control "public, no-transform"; 
        }

        location ~* \.(css|js)$ {
            gzip_static on;
        }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include snippets/fastcgi-php.conf;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param PHP_VALUE post_max_size=20M;
            fastcgi_param PHP_VALUE upload_max_filesize=20M;
        }
    }
}

最后,您需要在WordPress博客中change the HOME and SITEURL parameters指向该网站的新网址。