include /etc/nginx/conf.d/*.conf; # 配置在http{}下,nginx默认的配置,声明/etc/nginx/conf.d目录下的*.conf配置都生效
我把这句话注释之后,systemctl restart nginx,从另一台服务器curl发现是不通的
然后配置
include /etc/nginx/slb_conf/*.conf;
并且把conf.d下的default.conf复制到slb_conf下,再执行systemctl restart nginx,另一台服务器可以curl通了
太久没弄了,重新搞起,以下是在Windows下实现的
一、基本操作
查看版本号
nginx -v
启动nginx
nginx
停止nginx
nginx -s stop
重新加载nginx
nginx -s reload
测试nginx
nginx -t
二、配置反向代理
启动nginx后,浏览器输入127.0.0.1,默认访问的是html/index.html
proxy_pass https://www.baidu.com; # 在location下添加该配置,即可跳转到百度
# 主配置文件
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include ./nginx_conf/*.conf;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
proxy_pass https://www.baidu.com; # 在location下添加该配置,即可跳转到百度
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
再升级一下,配置127.0.0.1:8081跳转到QQ网
server {
listen 8081;
server_name localhost;
location / {
root html;
proxy_pass https://www.qq.com; # 在location下添加该配置,即可跳转到QQ
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
三、修改访问资源
1.在html这个文件夹下创建新文件夹Good,然后随便创建一个html
2.创建一个新的server块配置,如下
# 这是一个server块,写在主配置文件下或者include 指定的文件夹下均可
# 访问192.168.16.1:8080/Good时,会访问html/Good/Funny.html
server {
listen 8080;
server_name 192.168.16.1;
location / {
root html/Good;
index Funny.html;
}
}
四、问题总结
1.修改配置文件后,执行nginx -s reload 报错
"http" directive is not allowed here in xxx
原因:在主配置文件下,http块配置:< include ./nginx_conf/*.conf;>
意味着在./nginx/nginx_conf/*.conf
里的配置文件都会被加载到主配置文件的http
标签里去。所以如果在子配置文件里也有一个http
级的标签的话,就会报这个错误
处理方式:
./nginx/nginx_conf/下的conf,不要配置http,仅配置server即可
# nginx_conf下的一个子配置
server {
listen 8080;
server_name 192.168.16.1;
location / {
root html/Good;
index Funny.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2.截图
默认的conf ./HYH_File/App/下存放文件
events {
# 没有events会报错--> nginx: [emerg] no "events" section in configuration
}
http {
include mime.types;
include ./HYH_Conf/*.conf;server {
listen 8086;
server_name localhost; # 只配置2、3行会报404
location /App/ { # 访问http://localhost:8086/App/时,实际访问的是./HYH_File/App/下的Yes.html
# 访问http://localhost:8086/时,-->默认的index.html文件
root ./HYH_File/; # 到这个路径下找默认的index.html
index Yes.html; # 到root的路径下,默认打开的文件
# return 200 'Hello world!';
}
}
}