Nginx下配置虚拟主机:(基于IP地址,基于端口,基于域名)!反向代理!负载均衡!详细的那种!

1,先以root用户登录。找到/usr/local/nginx (默认安装到此处!)
在这里插入图片描述
sbin :存放二进制启动文件。往后每次进行配置变更后,都要来此处进行重启生效。
HTML:存放网页。有点了解应该都懂!
但是我们要在conf里进行配置。哈哈
在这里插入图片描述
2,进入conf目录。打开nginx.conf文件。进行编辑配置!
在这里插入图片描述
打开后,如图:
缩小了好多。便于整体浏览:
在这里插入图片描述
带#符号的行都是解释内容。
#user nobody; #配置允许运行nginx服务器的用户和用户组
worker_processes 1;#配置允许nginx进程生产的worker process数

#error_log logs/error.log; #配置nginx服务器运行对错误日志存放路径
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;//最大连接数,自己可以改
}

http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 16: request" ' #̲ …status b o d y b y t e s s e n t " body_bytes_sent " http_referer” ’
# ‘“ h t t p u s e r a g e n t " " http_user_agent" " http_x_forwarded_for”’;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;

server { #配置监听端口和主机名称
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /.ht {
# deny all;
#}
server {
listen 80;
server_name 192.168.158.100;
location / {
root /yuanlai;
index index.html index.htm;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
在最后一个大括号之前加上你要添加的虚拟IP。

server {
listen 80;
server_name 192.168.158.100;
location / {
root /yuanlai;
index index.html index.htm;
}
}
}
在这里插入图片描述
在重启Nginx时如果报错的话。一般都是大括号的事情!慢慢理理就可以找出来啦!

3,在ifconfig里加上与上面的另外的IP对应的:

ifconfig ens33:0 192.168.158.100 netmask 255.255.255.0 up
ens33是默认的!必须查询一下,这样才可以确保!
在这里插入图片描述

加上之后会发生改变:
在这里插入图片描述
3,关掉防火墙:
在这里插入图片描述
4,来验证一下吧!
192.168.158.134:第一个IP。
在这里插入图片描述
目录是HTML。就是Nginx下的。照片里有路径:
在这里插入图片描述
第二个:192.168.158.100照片里也有路径:
在这里插入图片描述
在这里插入图片描述
验证:
192.168.158.134
在这里插入图片描述
192.168.158.100
在这里插入图片描述
OK!

2,配置基于端口的虚拟主机并截图:
配置:(别的啥都不用变!)
在这里插入图片描述
然后重启:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3,配置基于域名的虚拟主机并截图:
在这里插入图片描述

修改window的hosts文件:(C:\Windows\System32\drivers\etc)
在这里插入图片描述
192.168.158.134 www.nginxniu.com
改WINDOWS的比较难。先以记事本保存到桌面。然后改名字,替换原来的hosts。
在这里插入图片描述
3,Nginx高级功能配置
1,配置Nginx反向代理并截图:
proxy_pass http://192.168.158.128
不要忘记“;”符号。
在这里插入图片描述
在这里插入图片描述

2,配置Nginx缓存服务器并截图:
在这里插入图片描述
在这里插入图片描述

OK!
3,配置Nginx负载均衡服务器并截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42859280/article/details/83038321