小编Jor*_*nLu的帖子

在 nginx 位置重写前缀 url

我的 nginx 配置文件是这样的:

server {
    listen 80;
    listen 443 ssl;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_error.log;
    access_log /log/nginx/xxx.com_access.log main;

    root /data/www/;
    index index.php index.html index.htm;

    location ~ \.php$ {
        add_header X-Frame-Options SAMEORIGIN;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们需要配置 nginx 以满足以下要求:

1?如果url没有前缀“/api/mobile/index.php”?并且请求的端口是80,重定向到https 2?如果url前缀“/api/mobile/index.php”?继续

所以我在配置文件中添加内容:

    location ~ ^(?!/api/mobile/index\.php).*$ {
        if ($server_port = "80") {
               return 301 https://$server_name$request_uri;
        }

        rewrite /* $server_name$reqeust_uri last;
    }
Run Code Online (Sandbox Code Playgroud)

现在配置文件内容是:

server {
    listen 80;
    listen 443 ssl;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_error.log;
    access_log …
Run Code Online (Sandbox Code Playgroud)

php nginx

10
推荐指数
1
解决办法
634
查看次数

标签 统计

nginx ×1

php ×1