nginx,www to non-ww,尝试了十几个解决方案



我有一个托管一个WordPress网站的小VP。我在Debian 12.04上使用Ngingx 1.4.4。我遵循我在网上找到的一些教程,这是我设法制作的。这是我的服务器块:

server {
listen  80;
#listen   [::]:80 default ipv6only=on; ## listen for ipv6
location ~*  .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
root /var/www;
index index.php index.html index.htm;
server_name bloggeri.es;
#Fix Yoast SEO Sitemaps
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;  
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
    rewrite ^(.+)$ /index.php?q=$1 last;
}
# Make site accessible from http://localhost/
location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    try_files $uri $uri/ /index.html;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}
location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
    # For example, return an error code
    #return 418;
#}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    try_files $uri =404;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
#   deny all;
#}
# Use gzip compression
gzip_static       on;  # Uncomment if you compiled Nginx using --with-http_gzip_static_module
gzip                on;
gzip_disable        "msie6";
gzip_vary           on;
gzip_proxied        any;
gzip_comp_level     5;
gzip_buffers        16 8k;
gzip_http_version   1.0;
gzip_types          text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png image/gif image/jpeg;

# Set a variable to work around the lack of nested conditionals
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $cache_uri 'no cache';
}   
if ($query_string != "") {
    set $cache_uri 'no cache';
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php)") {
    set $cache_uri "no cache";
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
    set $cache_uri 'no cache';
}
# Cache static files for as long as possible
location ~* .(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    try_files       $uri =404;
    expires         max;
    access_log      off;
}
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $cache_uri 'null cache';
}   
if ($query_string != "") {
    set $cache_uri 'null cache';
}   
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $cache_uri 'null cache';
}   
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
    set $cache_uri 'null cache';
}

}

有人有解决方案吗?我不知道这是一个很好的配置

诀窍是制作与www URL匹配并将其重定向到non-www URL的服务器块,在用实际域更换example.com后,将其添加到您的服务器块上方。

server {
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
  server_name example.com;
  # your actual config goes here
}

相关内容

最新更新