首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >少数几行Htaccess to Nginx转换

问少数几行Htaccess to Nginx转换
EN

Stack Overflow用户
提问于 2021-10-05 23:41:54
回答 1查看 47关注 0票数 0

我试过那些在线转换器,但都没有用。

如何将下面的htaccess转换为nginx规则。我只能从下面的代码中转换一行。我甚至不知道是对还是错。

我的nginx Conf

代码语言:javascript
复制
server {
    server_name example.com;
    root /var/www/example.com;

    index index.html index.htm index.php;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }
  
    location ~ /\.ht {
        deny all;
    }

这是我想要转换成nginx的代码。

代码语言:javascript
复制
    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # cors
    Header set Access-Control-Allow-Origin "*"
</IfModule>

# Hide a specific file
<Files .env>
    Order allow,deny
    Deny from all
</Files>

这是我唯一能转换的行。

代码语言:javascript
复制
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

至

代码语言:javascript
复制
try_files $uri $uri/ /index.php;

我仍然怀疑上面的nginx规则是否应该在冒号前包含、$args、?

请帮我这个忙。

EN

回答 1

Stack Overflow用户

发布于 2021-10-05 23:49:42

请注意,我使用了一个AI来协助这一转换。很想看看它是怎么做的。

代码语言:javascript
复制
# cors
add_header Access-Control-Allow-Origin *;

# gzip
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;

# cache
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

# security
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "no-referrer";
add_header X-Permitted-Cross-Domain-Policies "none";

# php
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69458353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档