我对Nginx相当陌生,我正在努力将。htaccess文件转换成Nginx可以理解的东西。一切都很好(大部分)——我可以把主页拉上来。问题是当我进入一个帖子页面。
与wordpress类似,url如下:
http://www.example.com/12/post-title-in-slug-form
其中12是post id,显然这个字符串是post的代码段。我试图将它们解析为两个单独的参数(id &slug)并将它们传递到index.php中,就像我在apache中成功地做的那样。我得到一个404页面,虽然,并已确认这是由于重写规则。下面是整个配置文件的样子,只有网站名称改变了(为了隐私):
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name example.com;
access_log off;
error_log on;
# deny access to .XYZ files
location ~ /'. {
return 403;
}
location ~ sitemap'.xml {
return 301 http://example.com/sitemap.php;
}
location ~ .php$ {
# Here you have to decide how to handle php
# Generic example configs below
# Uncomment and fix up one of the two options
# Option 1: Use FastCGI
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
try_files $uri $uri/ @router;
}
location @router {
rewrite ^/([0-9]+)/?(.*)?/?$ /index.php?id=$1&slug=$2 last;
}
}
}
请让我知道,如果你能发现什么扔它当它涉及到解析成id和鼻涕虫和传递它们的个别帖子。谢谢!
您应该在index.php前面添加/
和/
,如下所示:
rewrite ^/([0-9]+)/?(.*)?/?$ /index.php?id=$1&slug=$2 last;
注意我也用了$1
和$2
如果你发布的确实是完整的配置文件,那么设置缺少一些东西来处理PHP文件,因为regexp看起来很好。
我实际上认为你发布的配置不能是完整的,或者有一些基本的事情正在进行,因为配置应该抛出错误,未能加载,而且,既然你提到你的PHP加载良好,那么它不能是发布的配置服务于你的网站。
下面是一个更好的配置:仅供参考,try_files ABC XYZ last;
不是有效的语法,您需要在try_files中至少两个选项。无论如何,在发布的配置中也修复了这些问题。
server {
listen 80;
server_name example.com;
access_log off;
error_log on;
# deny access to .XYZ files
location ~ /'. {
return 403;
}
location ~ sitemap'.xml {
return 301 http://example.com/sitemap.php;
}
location ~ .php$ {
# Here you have to decide how to handle php
# Generic example configs below
# Uncomment and fix up one of the two options
# Option 1: Use FastCGI
#fastcgi_index index.php;
#include fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
# Option 2: Pass to Apache
# Proxy_pass localhost:APACHE_PORT
}
location / {
try_files $uri $uri/ @router;
}
location @router {
rewrite ^/([0-9]+)/?(.*)/?$ /index.php?id=$1&slug=$2 last;
}
}
您将需要修复PHP处理位,并选择要实现的设置。
我认为尽管你需要验证你只有一个nginx实例在运行,并且它是为你的网站服务的。