【问题标题】:Convert .htaccess rules to nginx.conf将 .htaccess 规则转换为 nginx.conf
【发布时间】:2014-01-19 21:54:43
【问题描述】:

您好,我正在尝试隐藏以下 .htaccess 行,以便箭头聊天可以在我的 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;
    #}
}

【问题讨论】:

  • 啊,我没注意到这里的赏金哈哈

标签: .htaccess mod-rewrite nginx


【解决方案1】:

好的,在朋友的帮助下,在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;
    }
}

【讨论】:

  • 其实这还是老配置。
  • @MohammadAbuShady 抱歉复制粘贴错误,已对其进行编辑以反映正确的代码 :)
  • 帮了我伙计!
猜你喜欢
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 2017-07-15
  • 2015-09-09
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
相关资源
最近更新 更多