官网下载安装包
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
安装
yum -i -c nginx-1.20.2-1.el7.ngx.x86_64.rpm
修改配置文件
vim /etc/nginx/nginx.conf
# 在http{}中,server{}之上加入如下内容
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
server {
listen 80;
server_name ip地址;
location / {
root /usr/local/blog/front/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /admin {
root /usr/local/blog/;
index index.html index.htm;
try_files $uri $uri/ /admin/index.html;
} # 配置后端访问地址
location ~ .*\.(git|jpg|pdf|jepg|png)$ {
root /home/plumemo/blog/;
}
location ^~ /api/blog {
index index.html index.htm index.php;
index proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index proxy_set_header Host $host;
index proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8086/api/plumemo-service; #后端服务器,配置upstream即可
}
}
nginx 常用操作
#启动nginx
systemctl start nginx
#停止 nginx
systemctl stop nginx
#重启 nginx
systemctl restart nginx
#重新加载配置
systemctl reload nginx
#设置开机启动
systemctl enable nginx
#关闭开机启动设置
systemctl disable nginx
#查看版本
nginx -V
#查看进程
ps -ef | grep nginx
#查看端口
netstat -nptl
nginx常用目录
|目录|说明|
|-|-|-|
| /etc/nginx/ | 所有相关配置文件目录 |
| /etc/nginx/nginx.conf | nginx主配置文件 |
| /etc/nginx/conf.d/ | 独立的nginx服务配置文件目录 |
| /var/log/nginx/ | nginx日志文件目录 |
| /var/log/nginx/access.log | 访问日志(IP/浏览器信息/处理时间/请求URL) |
| /var/log/nginx/error.log | 错误日志(服务器和请求处理中的错误信息) |
| /usr/share/nginx/html | 默认的站点位置,可以根据实际情况进行设置调整 |