我有以下服务器configuration:
server { listen 80; server_name mysite.proj; location / { root /path/to/mysite.proj/www; index index.php index.html index.htm; } access_log /path/to/mysite.proj/data/logs/access.log; error_log /path/to/mysite.proj/data/logs/error.log; if (!-e $request_filename) { rewrite ^(.+)$ /index.php last; } location ~ \.php$ { root /path/to/mysite.proj/www; fastcgi_pass 127.0.0.1:8081; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /path/to/mysite.proj/www$fastcgi_script_name; include fastcgi_params; } } server { listen 80; server_name www.mysite.proj; rewrite ^/(.*) http://mysite.proj/$1 permanent; }
它工作正常,每个url被重写为index.php。 但在同一时间,每个样式表url,每个JavaScripturl,每个图像url也被重写。 如何写重写规则不重写的CSS,JS,图像文件的URL?
您没有在服务器上下文中设置根,这是if的位置,所以它使用默认的<install prefix> / html。 您应该将根移动到服务器上下文,并将if转换为try_files。 另外,没有理由在no-wwwredirect中捕获请求,因为原始请求已存储在$ request_uri中。
server { listen 80; server_name www.mysite.proj; # Permanent redirect to no-www return 301 http://mysite.proj$request_uri; } server { listen 80; server_name mysite.proj; root /path/to/mysite.proj/www; index index.php index.html index.htm; access_log /path/to/mysite.proj/data/logs/access.log; error_log /path/to/mysite.proj/data/logs/error.log; location / { # try_files does not preserve query string by default like rewrite try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { # If the requested file doesn't exist, and /index.php doesn't, return a 404 try_files $uri /index.php =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:8081; } }