nginx安装与配置
1、执行下面依赖安装nginx安装与配置所需要的相关的环境
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2、下载nginx安装与配置压缩包
手动下载.tar.gz安装包,地址:https://nginx.org/en/download.html
3、上传到你喜欢的目录,然后解压。
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
4、开始安装nginx
使用默认配置,在nginx根目录下执行
./configure
make
make install
5、启动nginx
查找安装路径:
whereis nginx
然后切换到对应sbin目录下执行:
cd /usr/local/nginx/sbin/
./nginx 启动
浏览器访问ip+:80,页面会显示到nginx的欢迎页面。
6、nginx的配置,配置文件在conf目录下的nginx.conf文件,需要根据业务进行配置。
upstream lb{
server 172.16.88.131:90 weight=5; #权重
server 172.16.88.130:90 weight=1; #权重
}
# 服务1
server {
listen 80; #服务代理端口
server_name localhost;
location / {
proxy_pass http://lb; #lb来自上面的
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 服务2
server {
listen 8383;#服务代理前端端口
server_name localhost;
root /usr/local/nginx/html;
location / {
try_files $uri /index.html;
}
# 设置前缀
location /item/ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://172.16.88.131:80/item/;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_connect_timeout 300;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
上面这个是前后端分离的配置;