正向代理
server {
listen 8080;
resolver 8.8.8.8; # 使用外部DNS服务器,这里以Google的DNS为例,你也可以换成其他可靠的DNS
location / {
proxy_pass http://$http_host$request_uri;
proxy_set_header Host $http_host;
proxy_set_header X - Real - IP $remote_addr;
}
}
默认访问
![图片[1]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2023/12/1703950602-image.png)
配置如下:
[root@wj conf]# more nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
#http的网站所有配置都在此块
http {
#网站的全局设置,里面涉及的所有的网站都适用
include mime.types;#引用文件格式
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#server是每个网站的具体设置内容
#网站1 同一个地址 ---------------------看这里-----------------
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
#网站3 不同的地址
server {
listen 172.16.51.130:80; #要写成ip+端口的方式监听
location / {
root html3;
index index.html index.htm;
}
}
}
一个IP不同的端口访问
![图片[2]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2023/12/1703951437-image.png)
配置如下
[root@wj conf]# more nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
#http的网站所有配置都在此块
http {
#网站的全局设置,里面涉及的所有的网站都适用
include mime.types;#引用文件格式
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#server是每个网站的具体设置内容
#网站1 同一个地址
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
#网站3 不同的地址
server {
listen 172.16.51.130:80; #要写成ip+端口的方式监听
location / {
root html3;
index index.html index.htm;
}
}
}
不同的地址一个配置文件
![图片[3]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2023/12/1703995099-image.png)
配置如下
[root@wj conf]# more nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
#http的网站所有配置都在此块
http {
#网站的全局设置,里面涉及的所有的网站都适用
include mime.types;#引用文件格式
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#server是每个网站的具体设置内容
#网站1 同一个地址
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
server {
listen 80; #监控的网站端口
server_name 172.16.51.129; #网站的地址
location / {
root html; #网站的目录
index index.html index.htm; #网站的默认文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#网站2 同一个地址
server {
listen 8000; #新网站的端口
server_name 172.16.51.129; #新网站的地址,这个是同一个IP,不通的端口
location / {
root html2;
index index.html index.htm;
}
}
#网站3 不同的地址
server {
listen 172.16.51.130:80; #要写成ip+端口的方式监听
location / {
root html3;
index index.html index.htm;
}
}
}
服务器路径如下
[root@wj html]# pwd
/usr/local/nginx/html
[root@wj html]# more index.html
<meta charset=utf-8>
网站1
[root@wj html2]# pwd
/usr/local/nginx/html2
[root@wj html2]# more index.html
<meta charset=utf-8>
网站2
[root@wj html3]# pwd
/usr/local/nginx/html3
[root@wj html3]# more index.html
<meta charset=utf-8>
网站3
配置多目录一个IP和端口
![图片[4]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707189003-image.png)
配置文件如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#nginx默认访问这个路径 ip+端口
location / {
root html;
index index.html index.htm;
}
location /wj { ###########################################主要看这里
alias /usr/local/nginx/wj; #这是配置的目录
index a.html a.htm; #这是目录下的默认文件
}
...............后面省略........................
服务器目录如下:
[root@wj wj]# pwd
/usr/local/nginx/wj
[root@wj wj]# ls
a.html
重定向
#重定向(301永久)
将 www.example.com 重定向到 example.com
server {
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
#重定向(302临时)
server {
listen 80;
server_name yourdomain.com;
return 302 http://otherdomain.com;
}
#重定向到https(方式1)
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
#重定向到https(方式2)
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
重定向到https总结区别:
第一个配置将请求重定向到同一域名下的HTTPS版本。
第二个配置将请求重定向到指定的新域名(example.com)上的相同路径。
选择哪一个取决于你的需求。如果你正在迁移整个站点到一个新的域名,那么第二个配置可能更适合。如果你只是想将HTTP流量重定向到HTTPS流量(在同一域名下),那么第一个配置可能是你需要的。
默认访问
![图片[5]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707199900-image-1024x359.png)
重定向后再次刷新访问
![图片[6]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707199933-image.png)
配置如下:
![图片[7]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707200068-image.png)
或者这么写,可以跳转到某个网站
![图片[8]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707200255-image.png)
静止谷歌浏览器访问
![图片[9]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707201678-image-1024x208.png)
配置如下:
![图片[10]-nginx应用场景-秋风落叶](https://wangjian.run/wp-content/uploads/2024/02/1707201728-image.png)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容