干活中需要配置一个Nginx实现反向代理。
当然,Nginx还有其他应用:
- http服务器,访问静态资源
- 虚拟主机
- 反向代理、负载均衡
部署Nginx
Nginx有两种部署方式:yum和离线编译,一般情况下使用yum部署即可。
- yum部署Nginx
yum install nginx -y
- 但可能会遇到报错
没有可用软件包 nginx。
原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面,解决方法:
yum install epel-release
https://blog.csdn.net/zhou_438/article/details/89554438 - 离线部署
离线部署就是在Linux上手动编译一个Nginx了,中间要的依赖比较多,可以参考:
https://developer.aliyun.com/article/791648
配置Nginx
https://blog.csdn.net/Orainge/article/details/129034901
- Nginx的配置文件有哪些?
Nginx通过nginx.conf定义默认配置文件,也可以自定义任意文件来配置,这些配置文件都会同时生效。 - nginx.conf的含义
通过yum部署的nginx.conf默认位置在/etc/nginx/nginx.conf
Paramenter | Effect |
---|---|
user | 设置worker进程的用户,可以设置为root |
worker_processes | worker进程工作数,可以设置为cpu数,默认自动 |
error_log | 日志级别 |
pid | nginx进程pid |
include | 引入外部配置,写上路径,可以用通配符,可以写多行 |
events{} | 影响 Nginx 服务器与用户的网络连接 |
http{} | 包括http块、server块、location块 |
http块
Parameter | Effect |
---|---|
log_format | 设置输出日志的格式 |
access_log | 日志位置和使用格式 |
sendfile | 高效文件传输 |
tcp_nopush | 高效文件传输 |
tcp_nodelay | 高效文件传输 |
keepalive_timeout | 设置请求超时时间 |
types_hash_max_size | 有关服务器名字hash表查询 |
include | 指定包含其他拓展配置文件 |
default_type | 其他文件类型的默认处理方式(下载or打开)默认下载 |
server块
Parameter | Effect |
---|---|
listen | 监听的ip:端口 |
server_name | 虚拟服务器域名,匹配到特定的server块,转发到对应的应用服务器 |
root | 默认根目录 |
include | 默认外挂文件 |
error_page | 默认错误页 |
location | 匹配对应的用户请求url,并将其映射到服务器的指定目录 |
Tips
- 通过yum安装其文件位置
文件名称 | 文件作用 | 文件路径 |
---|---|---|
nginx.conf | 启动配置文件 | /etc/nginx/nginx.conf |
conf.d | 1111 | /etc/nginx/conf.d |
html/* | 网页服务器项目文件 | /usr/share/nginx/html/ |
logs | 日志文件 | /var/log/nginx/ |
其他安装文件 | 其他安装文件 | /etc/nginx |
systemctl start nginx 报错
ob for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details
执行两个命令后发现http权限不足,可以参考https://blog.csdn.net/RunSnail2018/article/details/81185138systemctl stop nginx 报错
只有通过systemctl启动的nginx可以通过systemctl关闭或重启