NGINX nice url 配置

NGINX nice url configuration

提问人:Lukáš Ježek 提问时间:1/20/2023 更新时间:1/20/2023 访问量:29

问:

在 NGINX 上,我试图弄清楚如何在重定向到 php 文件后在地址栏中保留漂亮的 URL。我已经在代码中尝试了下面提到的所有方法,它工作正常,但无论是使用重定向还是代理,我仍然在地址栏中看到 www.mydomain.com/products.php。我需要保持地址看起来 www.mydomain.com/products。有什么想法吗?

server {
server_name mydomain.com www.mydomain.com;
    root /var/www/mydomain.com;

index index.html index.php;

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

location /products {
    #rewrite ^/products$ /products.php last;
    #try_files $uri $uri/ /products.php;
    proxy_pass http://localhost/products.php;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

}

nginx 友好网址

评论


答:

0赞 Lukáš Ježek 1/20/2023 #1

解决了,我太复杂了,最简单的方法就可以了。

location /products {
  rewrite ^/products /products.php last;
}