Nginx 重写模块不工作

Nginx 重写模块不工作

我读了很多关于它的主题,但无法解决我的问题。

从 APache 迁移到 Nginx 后,我无法设置重写来处理我的论坛友好 URL。

我的论坛位于虚拟服务器的 /forum/ 位置。我要做的是将 /forum/thred-something.html&someaction 重写为 /forum/showthread.php?tid=ID&action=someaction

我使用这个插件进行名称到 ID 的转换:https://github.com/frostschutz/MyBB-Google-SEO

我的虚拟服务器本地化配置如下:

server {
    server_name www.podziemieopowiadan.pl;
    listen 80;

    return 301 http://podziemieopowiadan.pl/index.php;
}


server 
{
    server_name podziemieopowiadan.pl;
    listen 80;

    access_log /var/log/nginx/podziemieopowiadan-pl_access_log;
    error_log /var/log/nginx/podziemieopowiadan-pl_error_log;
    root /var/www/domains/podziemieopowiadan-old;

    index index.php;

    allow 188.127.246.50;
    allow 188.127.242.122;
    allow 204.93.240.0;
    allow 204.93.177.0;
    allow 199.27.128.0;
    allow 173.245.48.0;
    allow 103.21.244.0;
    allow 103.22.200.0;
    allow 103.31.4.0;
    allow 141.101.64.0;
    allow 108.162.192.0;
    allow 190.93.240.0;
    allow 188.114.96.0;
    allow 197.234.240.0;
    allow 198.41.128.0;
    allow 162.158.0.0;

    include global.conf;

    location /forum/{
        index index.html index.php;

    rewrite ^/([^&]*)&(.*)$ http://podziemieopowiadan.pl/forum/$1?$2 permanent;

    rewrite ^/((?i)sitemap-([^./]+)\.xml)$ /forum/misc.php?google_seo_sitemap=$2 last;
    rewrite ^/((?i)forum-([^./]+)\.html)$ /forum/forumdisplay.php?google_seo_forum=$2 last;
    rewrite ^/((?i)thread-([^./]+)\.html)$ /forum/showthread.php?google_seo_thread=$2 last;
    rewrite ^/((?i)announcement-([^./]+)\.html)$ /forum/announcements.php?google_seo_announcement=$2 last;
    rewrite ^/((?i)user-([^./]+)\.html)$ /forum/member.php?action=profile&google_seo_user=$2 last;
    rewrite ^/((?i)calendar-([^./]+)\.html)$ /forum/calendar.php?google_seo_calendar=$2 last;
    rewrite ^/((?i)event-([^./]+)\.html)$ /forum/calendar.php?action=event&google_seo_event=$2 last;
    }

    location ~ \.php$
    {
               try_files $uri =404;
               include fastcgi_params;
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_param REDIRECT_URL $request_uri;
    }

}

这是我尝试重新创建的 .htaccess 代码(由插件本身建议):

RewriteEngine on
RewriteBase /forum/

RewriteRule ^([^&]*)&(.*)$ http://podziemieopowiadan.pl/forum/$1?$2 [L,QSA,R=301]

ErrorDocument 404 /forum/misc.php?google_seo_error=404
RewriteRule ^sitemap\-([^./]+)\.xml$ misc.php?google_seo_sitemap=$1 [L,QSA,NC]
RewriteRule ^forum\-([^./]+)\.html$ forumdisplay.php?google_seo_forum=$1 [L,QSA,NC]
RewriteRule ^thread\-([^./]+)\.html$ showthread.php?google_seo_thread=$1 [L,QSA,NC]
RewriteRule ^announcement\-([^./]+)\.html$ announcements.php?google_seo_announcement=$1 [L,QSA,NC]
RewriteRule ^user\-([^./]+)\.html$ member.php?action=profile&google_seo_user=$1 [L,QSA,NC]
RewriteRule ^calendar\-([^./]+)\.html$ calendar.php?google_seo_calendar=$1 [L,QSA,NC]
RewriteRule ^event\-([^./]+)\.html$ calendar.php?action=event&google_seo_event=$1 [L,QSA,NC]

我的nginx.conf:

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 256;
    # 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;
    client_max_body_size 2m;

    # 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/*;

    ##
    # Page Speed Configuration
    ##

    #pagespeed on;
    #pagespeed FileCachePath /var/ngx_pagespeed_cache;
    #pagespeed CreateSharedMemoryMetadataCache "/var/ngx_pagespeed_cache" 102400;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

我的global.conf:

# Ustawianie naglowkow expires
location ~* \.(?:ico|js|gif|jpe?g|png)$ 
{
    expires 24h;
    access_log off;
    log_not_found off;
}


# Blokada dostepu + wylaczanie logowania
location = /robots.txt  { access_log off; log_not_found off; }
location ~ /\.          { access_log off; log_not_found off; deny all; }
location ~ ~$           { access_log off; log_not_found off; deny all; }

    # Cloudflare
    set_real_ip_from 204.93.240.0/24;
    set_real_ip_from 204.93.177.0/24;
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    real_ip_header CF-Connecting-IP;    

# Blokada dostepu do specyficznych danych
#location ~ cache/(.*)$
#{
#    deny all;
#}

答案1

这里的问题是rewrite指令目标部分始终与root目录相关。

因此,假设您的设置如下:

root目录是/var/www/domains/podziemieopowiadan-old

论坛脚本位于/var/www/domains/podziemieopowiadan-old/forum

那么你的rewrite需求看起来应该是这样的:

rewrite ^/forum/((?i)sitemap-([^./]+)\.xml)$ /forum/misc.php?google_seo_sitemap=$2 last;

也就是说,添加相对于指令root中的目录的目录rewrite

另一种方法是将其添加alias /var/www/domains/podziemieopowiadan-old/forumlocation /forum块中。不过这可能会产生其他副作用。

相关内容