将.htaccess规则转换为nginx.conf

时间:2014-01-02 12:36:56

标签: .htaccess mod-rewrite nginx

您好我正在尝试隐藏以下 .htaccess 行,以便arrowchat可以在我的nginx服务器中运行。我的arrowchat文件夹的位置就像这个/usr/share/nginx/html/arrowchat/,其中/usr/share/nginx/html/是我网站的根文件夹。我的VPS上只有一个网站 www.jukpac.com 。以下是 .htaccess 代码

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^chatroom ^/../public/chatroom/ [L]
    RewriteRule ^cron ^/../public/cron/ [L]
    RewriteRule ^debug ^/../public/debug/ [L]
    RewriteRule ^list ^/../public/list/ [L]
    RewriteRule ^mobile ^/../public/mobile/ [L]
    RewriteRule ^popout ^/../public/popout/ [L]
    RewriteRule ^video ^/../public/video/ [L]
</IfModule>

<IfModule mod_headers.c>
 <FilesMatch "\.(gif|jpg|png|css|swf)$">
  Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT"
  Header add "Cache-Control" "max-age=31536000"
 </FilesMatch>
</IfModule>

<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault A604800
 ExpiresByType text/css A604800
 ExpiresByType image/gif A604800
 ExpiresByType image/png A604800
 ExpiresByType image/jpeg A604800
 ExpiresByType application/x-shockwave-flash A604800
</IfModule>

我尝试了一些在线.htaccess转换器,但它没有用,可以有人请让我知道如何解决这个问题?

我当前的nginx default.conf文件看起来像这样

#
# The default server
#
server {
    listen       80;
    server_name  jukpac.com;
    return       301 http://www.jukpac.com$request_uri;
}

server {
    listen       80;
    server_name  www.jukpac.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }
    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/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/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

1 个答案:

答案 0 :(得分:4)

在朋友的帮助下,在Serverfault中我设法获得了正确的配置,如下所示

    server { #redirecting server
    listen       80;
    server_name  jukpac.com;
    return       301 http://www.jukpac.com$request_uri;
}

server {
    listen       80;
    server_name  www.jukpac.com;
    root   /usr/share/nginx/html; #move the root to server level
    index  index.php # move index to server level
    error_page  404  /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
    } 

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}