我有一点困难。 我写了一个自定义的PHP MVC框架,并在其上构build了一个CMS。 我决定给nginx + fpm一个旋转。 这是我困境的根源。 我被要求在我的网站上join一个wordpress博客(yah)。它有很多内容,在短时间内我不得不将所有的内容放到我的CMS中。 由于使用了Apache多年,我承认,使用nginx会有一点损失。
我的网站有文件path:
/opt/directories/mysite/public/
wordpress文件位于:
/opt/directories/mysite/news/
我知道我只需要设置target / news [/ *]的位置,然后强制所有匹配的URI到index.php中。 有人可能指向我正确的方向吗?
我的configuration如下:
server { listen 80; server_name staging.mysite.com index index.php; root /opt/directories/mysite/public; access_log /var/log/nginx/mysite/access.log; error_log /var/log/nginx/mysite/error.log; add_header X-NodeName directory01; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location / { try_files $uri $uri/ /index.php?route=$uri&$args; } location ~ /news { try_files $uri $uri/ @news; } location @news { fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_split_path_info ^(/news)(/.*)$; fastcgi_param SCRIPT_FILENAME /opt/directories/mysite/news/index.php; fastcgi_param PATH_INFO $fastcgi_path_info; } include fastcgi_params; include php.conf; location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ { access_log off; expires 30d; } ## Disable viewing .htaccess & .htpassword location ~ /\.ht { deny all; } }
我的php.conf文件:
location ~ \.php { fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_pass unix:/tmp/php-fpm.sock; # If you must use PATH_INFO and PATH_TRANSLATED then add # the following within your location block above # (make sure $ does not exist after \.php or /index.php/some/path/ will not match): #fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_param PATH_INFO $fastcgi_path_info; #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; }
fastcgi_params文件:
fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on;
大部分感谢@Kromey,我调整了我的位置/新闻/但我仍然没有得到所需的结果。
当我发现我的php位置被首先匹配时,我能够学习到我/新闻位置。
有了这个设置,我现在得到了200的状态,但是页面是空白的。 有任何想法吗?
这实际上是相当简单的,而且比Apache上的Wordpress类似的设置更加紧凑(我运行了2个Wordpress博客,之前都在Apache上,最近都迁移到了nginx + PHP(不是FPM,虽然我确实有一段时间才决定它是我的低stream量网站矫枉过正))。
location /news/ { #Need a new root to point to your "aliased" directory root /opt/directories/news # This is cool because no php is touched for static content try_files $uri $uri/ /index.php; }
正如评论所暗示的那样,任何静态内容(图片,CSS,JavaScript,robots.txt等),在您的Wordpresspath内直接得到服务,没有PHP的参与; 一切都被路由到index.php。
请注意, try_files
指令是半新(0.8我认为?),所以你可能不得不升级你的nginx; 当我从Ubuntu apt版本库中安装的时候,版本太旧了,configuration也变得非常复杂,以至于我apt-get remove
它从源代码中apt-get remove
并安装nginx(一个相当简单的操作)。