1

I've read a lot of topics about it, but couldn't solve my problem.

After moving from APache to Nginx I can't set rewrites to handle my forums friendly urls.

My forums is in /forum/ location of the virtual server. All I want to do is rewrite /forum/thred-something.html&someaction onto /forum/showthread.php?tid=ID&action=someaction

I use this plugin for name-to-id transaltion: https://github.com/frostschutz/MyBB-Google-SEO

That's how my virtual-server localization config looks like:

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;
    }

}

And here's the .htaccess code I was trying to recreate (suggested by plugin itself):

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]

My 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;
#   }
#}

And my 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;
#}
2
  • Please post the full nginx configuration in the question, it contains things that are needed to properly answer the question. Jan 24, 2015 at 13:07
  • First post updated.
    – Nidrax
    Jan 24, 2015 at 13:18

1 Answer 1

0

The issue here is that rewrite directive destination part is always relative to the root directory.

So, assuming your setup is like this:

root directory is /var/www/domains/podziemieopowiadan-old

Forum scripts are at /var/www/domains/podziemieopowiadan-old/forum

Then your rewrite needs to look like this:

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

That is, add the directory relative to the root directory in the rewrite directive.

An alternative could be to add alias /var/www/domains/podziemieopowiadan-old/forum to the location /forum block. This could have other side effects though.

6
  • I added the /forum to the rewrite rules, as you stated, but still get an 404 when trying to go to the location.
    – Nidrax
    Jan 24, 2015 at 13:28
  • What does nginx error.log show? Jan 24, 2015 at 13:33
  • Nothing really relevant I guess. 2015/01/24 08:54:02 [error] 6759#0: *6 open() "/var/www/domains/podziemieopowiadan-old/forum/thread-regulamin-ekipy.html" failed (2: No such file or directory), client: 79.186.213.116, server: podziemieopowiadan.pl, request: "GET /forum/thread-regulamin-ekipy.html?action=lastpost HTTP/1.1", host: "podziemieopowiadan.pl", referrer: "podziemieopowiadan.pl/forum"
    – Nidrax
    Jan 24, 2015 at 13:55
  • It juust looks like the server is ignoring the rewrites completely.
    – Nidrax
    Jan 24, 2015 at 13:56
  • Okay, I fixed it. Just needed to also put /forum in the beggining of rule. So it had to be rewrite ^/forum/((?i)
    – Nidrax
    Jan 24, 2015 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .