location ~ \.php$ { root /home/website/wordpress; fastcgi_pass unix:/usr/local/php/var/run/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; index index.html index.php; if (-f $request_filename/index.html){ rewrite (.) /index.html break; } if (-f $request_filename/index.php){ rewrite (.) /index.php; } if (!-f $request_filename){ rewrite (.) /index.php; } }
内容干系链接
这部分是spa项目实现的核心部分了,一样平常是首页,列表页(分页、标签、日期等),内容详情页
location ~ ^\/($|(category|tag|date|page)\/|\d+\.html$) { proxy_pass http://localhost:8800; if ($http_user_agent ~ (SemrushBot|Semrush) ) { return 410; }}
自己用spa实现了多少,自己最清楚,实现了的走spa路由。
根据链接的来源,可以分成以下几类:

location / { index index.html index.php; root /home/website/wordpress; try_files $uri $uri/ /index.php;}rewrite /wp-admin$ $scheme://$host$uri/ permanent;
静态资源如果是wordpress后台系统须要的,就连续走wordpress了,如果是spa里面自己就须要的资源,比如首页对外公布的微信群二维码https://abc.com/wechat.png。这类资源建议放统一的文件夹内,方便在nginx里面配置,比如换成https://abc.com/res/wechat.png。必须以根路径对外的资源链接,比如一些平台里面站长认证资源,https://abc.com/baidu_verify_9T2XP6KRil.html,这里自己没法调度访问url的,在配置nginx的时候就很麻烦了,总不能一个一个设置吧。。。这个值得作难堪点仔细讲一讲。静态资源配置难点
这里我们紧张将上面提到的必须以根路径对外的资源链接,紧张是一些站长认证资源,由于其它能让我们自主调度访问url的,都已经归到静态资源的第2点了,对付难搞的第3点,我们可以将这些认证文件(比如baidu_verify_9T2XP6KRil.html)直接放到wordpress项目根目录,但是既然用了spa,我们一样平常是希望由更多的部分由spa还处理的。
我们可以让nginx先在spa的静态资源里面找,找不到了在交由wordpress处理。
# /xxx 或者 /xxx.yyylocation ~ ^\/\w+\.?\w+$ { root /home/website/spa/static; try_files $uri @wordpress;}location @wordpress { index index.html index.php; root /home/website/wordpress; try_files $uri $uri/ /index.php;}
我们可以在nginx官网看看try_files的用法
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. For example:
location /images/ { try_files $uri /images/default.gif; } location = /images/default.gif { expires 30s; }
The last parameter can also point to a named location, as shown in examples below. Starting from version 0.7.51, the last parameter can also be a code:
location / { try_files $uri $uri/index.html $uri.html =404; }
Example in proxying Mongrel:
location / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
完全配置# wordpress phplocation ~ \.php$ { root /home/website/wordpress; fastcgi_pass unix:/usr/local/php/var/run/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; index index.html index.php; if (-f $request_filename/index.html){ rewrite (.) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.) $1/index.php; } if (!-f $request_filename){ rewrite (.) /index.php; } } # spa location ~ ^\/($|(category|tag|date|page)\/|\d+\.html$) { proxy_pass http://localhost:8800; if ($http_user_agent ~ (SemrushBot|Semrush) ) { return 410; }}# wordpresslocation / { index index.html index.php; root /home/website/wordpress; try_files $uri $uri/ /index.php;}rewrite /wp-admin$ $scheme://$host$uri/ permanent;# /xxx 或者 /xxx.yyylocation ~ ^\/\w+\.?\w+$ { root /home/website/spa/static; try_files $uri @wordpress;}location @wordpress { index index.html index.php; root /home/website/wordpress; try_files $uri $uri/ /index.php;}