配置网络源

配置nginx源

安装nginx

启动服务

修改nginx主页
![]()
重启访问测试

修改hosts文件

![]()
修改主配置文件

server {
listen 192.168.100.10:80 default_server;
# listen [::]:80 default_server;
server_name www.newbenet.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
if ($host = 'www.benet.com') {
rewrite ^/(.*)$ http://www.newbenet.com/$1 permanent;
}
}
}
客户端抓包工具访问测试
旧域名访问

显示结果

旧域名会自动改变成新域名
配置地址带参跳转
正常访问

修改配置文件 禁止访问后台

客户端测试

强制跳转

客户端测试

源代码安装的nginx 地址重写
修改主配置文件测试
![]()


修改主配置文件配置旧域名访问
![]()

server {
listen 192.168.100.20:80;
server_name www.newbenet.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.100.20:80;
server_name www.benet.com;
location / {
if ($host = 'www.benet.com') {
rewrite ^/(.*)$ http://www.newbenet.com/$1 permanent;
}
}
}
客户端测试


抓包工具测试

配置带参数跳转
创建网站根目录 前后台页面

修改主配置文件
![]()

配置hosts文件测试访问

客户端访问后台测试 http://www.benet.com/test.html

禁止访问后台

server {
listen 192.168.100.20:80;
server_name www.benet.com;
location / {
root /var/www/;
index index.html index.htm;
if ( $request_uri ~ ^/test.html$ ){
rewrite (.*) http://www.benet.com permanent;
}
}
客户端测试 http://www.benet.com/test.html
访问特定网站根目录数据执行跳转
网站根目录创建测试数据

测试正常访问

修改nginx主配置文件访问特定目录数据执行跳转

location / {
root /var/www/;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.benet.com permanent;
}
访问测试
http://www.benet.com/upload

本文详细介绍了Nginx的配置过程,包括域名重定向、地址带参跳转、禁止访问特定路径等功能,并提供了具体的配置示例。
845

被折叠的 条评论
为什么被折叠?



