nginx问题 不同域名实现跳转,原域名访问url不变,好不好配置nginx?

1个回答

写回答

Zb1121

2026-01-30 13:02

+ 关注

php
php

要实现不同域名实现跳转,可以使用Nginx的"location"指令。以下是一个示例配置:nginxserver { listen 80; server_name example.com www.example.com; root /path/to/example.com/html; index index.html; location / { try_files $uri $uri/ /index.html; } location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}以上配置会将所有请求都转发到example.com,并且如果存在.php扩展名的请求,则使用FastCGI方式处理。如果要实现其他域名进行跳转,只需在"location"指令中添加相应的域名即可。例如,如果要将www.example.com的请求转发到example.com,可以添加以下配置:nginxserver { listen 80; server_name example.com www.example.com; root /path/to/example.com/html; index index.html; location / { try_files $uri $uri/ /index.html; } location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}server { listen 80; server_name www.example.com; root /path/to/example.com/html; index index.html; location / { try_files $uri $uri/ /index.html; }}以上配置会将www.example.com的请求直接转发到example.com的根目录下。这样就实现了不同域名的跳转功能。

举报有用(0分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号