> 文章列表 > Nginx学习笔记 - 新

Nginx学习笔记 - 新

Nginx学习笔记 - 新

跟着 https://www.bilibili.com/video/BV1yS4y1N76R 视频学的
安装教程 nginx环境搭建

通过不同域名相同端口访问不同页面

首先添加hosts,映射下域名到装nginx的主机IP地址,我这里是虚拟机,且没有买域名就自己本地这样玩
Nginx学习笔记 - 新

进入nginx安装目录,打开nginx.conf,添加俩虚拟主机server

	#代表如果是www.sb.com:81访问的,则会访问/usr/local/nginx/www/www/www.html页面server {listen       81; #监听81端口server_name  www.sb.com;#通过域名www.sb.com访问location / {root	www/www;#开头没斜杠,代表相对路径,即nginx根目录下的www/www目录index	www.html;#index 的作用就是当没有访问任何文件时,则默认访问 index.html}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}#代表如果是sss.sb.com:81访问的,则会访问/usr/local/nginx/www/sss/sss.html页面server {listen       81;server_name  sss.sb.com;location / {root	www/sss;index sss.html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

配置完重启服务生效:systemctl reload nginx
访问,如果中文乱码按照这个来nginx乱码问题文章解决
Nginx学习笔记 - 新
Nginx学习笔记 - 新

反向代理 - 请求外网或内网主机

使用 proxy_pass 配置

到外网

比如访问nginx的ip:192.168.80.131,会重定向到百度,浏览器地址也会调到百度

location / {proxy_pass http://www.baidu.com/; 表示页面重定向到百度index  index.html index.htm;
}

到内网其他主机

比如又开了一台nginx,IP是192.168.80.132
我想从192.168.80.131访问到192.168.80.132

    location / {proxy_pass http://192.168.80.132;index  index.html index.htm;}

Nginx学习笔记 - 新