我试图得到一个重写规则为nginx和Wordpress工作。 在重新启动nginx之后,我尝试了这个站点,并得到一个IS 500错误页面。 日志文件说:
*1 rewrite or internal redirection cycle while internally redirecting to "/index.php"
我很困惑,因为这是他们如何告诉你在所有的教程中(甚至是用于设置nginx的重写规则的Wordpress文档):
try_files $uri $uri/ /index.php?$args ;
这是整个服务器块:
server { listen 178.79.134.35:443 http2; listen [::]:443 http2; server_name foo.co.uk www.foo.co.uk cdn.foo.co.uk; ssl on; ssl_certificate /home/test/conf/web/ssl.foo.co.uk.pem; ssl_certificate_key /home/test/conf/web/ssl.foo.co.uk.key; error_log /var/log/apache2/domains/foo.co.uk.error.log error; location / { # This is cool because no php is touched for static content. # include the "?$args" part so non-default permalinks doesn't break when using query string try_files $uri $uri/ /index.php?$args ; proxy_pass https://178.79.134.35:8443; location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ { root /home/test/web/foo.co.uk/public_html; access_log /var/log/apache2/domains/foo.co.uk.log combined; access_log /var/log/apache2/domains/foo.co.uk.bytes bytes; expires max; try_files $uri @fallback; } } location /error/ { alias /home/test/web/foo.co.uk/document_errors/; } location @fallback { proxy_pass https://178.79.134.35:8443; } location ~ /\.ht {return 404;} location ~ /\.svn/ {return 404;} location ~ /\.git/ {return 404;} location ~ /\.hg/ {return 404;} location ~ /\.bzr/ {return 404;} include /home/test/conf/web/snginx.foo.co.uk.conf*; }
任何人有任何build议? 我甚至尝试了一些更杂乱的东西,但得到同样的错误:
if (!-e $request_filename){ rewrite ^(.*)$ /index.php?q=$1 last; break; }
我已经调整了一下,所以它传递给了
location / { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/test/web/foo.co.uk/public_shtml$fastcgi_script_name; include fastcgi_params; try_files $uri $uri/ /index.php?$args ; proxy_pass https://178.79.134.35:8443; location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ { root /home/test/web/foo.co.uk/public_shtml; access_log /var/log/apache2/domains/foo.co.uk.log combined; access_log /var/log/apache2/domains/foo.co.uk.bytes bytes; expires max; try_files $uri @fallback; } }
更新2:如所示,我知道一个location ~ \.php$ { }块如何包裹它。 所以它看起来像:
location / { proxy_pass https://178.79.134.35:8443; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME home/test/web/foo.co.uk/public_shtml$fastcgi_script_name; include fastcgi_params; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ { root /home/test/web/foo.co.uk/public_shtml; access_log /var/log/apache2/domains/foo.co.uk.log combined; access_log /var/log/apache2/domains/foo.co.uk.bytes bytes; expires max; try_files $uri @fallback; } try_files $uri $uri/ /index.php?$args ; }
但我现在得到一个错误:
111:连接被拒绝)连接到上游,客户端:81.174.134.133,服务器:foo.co.uk,请求:“GET / wp-admin / HTTP / 2.0”,上游:“fastcgi://127.0.0.1:9000 “
看着谷歌,人们build议testing端口9000:
root@com:/home/# telnet localhost 9000 Trying ::1... Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused
所以它似乎无法访问它。 我有点困惑,因为防火墙设置为允许端口9000:
ACCEPT tcp -- anywhere anywhere tcp dpt:9000
更新2:如build议,我试过:
netstat -nltp|grep 9000
…但没有结果。 如果我看,但我可以看到安装和运行的PHP-7.0-FPM(据我所知);
root@com:~# service php7.0-fpm status â php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2017-04-12 12:06:48 UTC; 18h ago Process: 3560 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS) Main PID: 3800 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /system.slice/php7.0-fpm.service ââ3800 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ââ3872 php-fpm: pool www ââ3882 php-fpm: pool www Apr 12 12:06:43 com.x.com systemd[1]: Starting The PHP 7.0 FastCGI Process Manager... Apr 12 12:06:48 com.x.com systemd[1]: Started The PHP 7.0 FastCGI Process Manager.
我查看/etc/php/7.0/fpm/php-fpm.conf ,但没有看到任何引用“侦听”端口
更新3:我find了一篇关于编辑www.conf文件的post: https : //github.com/serghey-rodin/vesta/issues/1025
所以在我的服务器上寻找,我发现:
/etc/php/7.0/fpm/pool.d/www.conf
然后我评论说:
listen = /run/php/php7.0-fpm.sock
…并补充道:
listen = 9000
…然后重新启动:
service php7.0-fpm restart
现在我可以在netstat上看到它
然后我确定我有这个在我的configuration:
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/test/web/foo.co.uk/public_shtml$fastcgi_script_name; include fastcgi_params; }
重新启动nginx,瞧:)它的生活!
在PHP项目中使用Nginx有两种常见的方法:
这是设置专用服务器以获得最佳性能的常见场景,您应该使用它,除非您的应用程序严重依赖于某些Apache的function(例如.htaccess并且您无法在Nginxconfiguration中一次又一次地完成此操作。
server { ... root /home/test/web/foo.co.uk/public_html; fastcgi_index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ { access_log /var/log/apache2/domains/foo.co.uk.log combined; access_log /var/log/apache2/domains/foo.co.uk.bytes bytes; expires max; } }
在这种情况下,PHP处理由Apaches mod_php模块完成。 设置是这样的:
server { ... root /home/test/web/foo.co.uk/public_html; location / { proxy_pass https://178.79.134.35:8443; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ { access_log /var/log/apache2/domains/foo.co.uk.log combined; access_log /var/log/apache2/domains/foo.co.uk.bytes bytes; expires max; } }
除非你想要你的apache生成一个沉重的404页面,否则没有必要在后备代码中使用静态文件。 这也可以通过error_page指令在nginx中设置。
另外,在这种情况下,请考虑在普通的HTTP端口的环回接口上连接到Apache,以获得更好的性能。 只能在前端Web服务器上设置HTTPS 。 在同一主机上代理启用HTTPS的端口并不能真正为您带来任何好处。
更新 :我只是为了举例而在我的示例中使用了TCP套接字连接到PHP-FPM。 但是,Internet上有很多基准testing,对于本地UNIX套接字而不是TCP套接字,performance出更好的性能。 对于Nginx来说,使用UNIX套接字也相当简单:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
确保检查对该文件和相应path的访问权限。