一、在nginx官方下载nginx地址 nginx: download
二、下载完成后 用:
tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/
三、解压完成后我们需要编译安装:
配置:使用默认配置,在nginx根目录下执行
./configure make make install
查询安装路径 : whereis nginx
四、运行nginx程序
首先切换到nginx 安装目录下,然后cd nginx/sbin目录下,然后用
./nginx 命令启动nginx
五、配置自动重启nginx
# 开启 service firewalld start # 重启 service firewalld restart # 关闭 service firewalld stop # 查看防火墙规则 firewall-cmd --list-all # 查询端口是否开放 firewall-cmd --query-port=8080/tcp # 开放80端口 firewall-cmd --permanent --add-port=80/tcp # 移除端口 firewall-cmd --permanent --remove-port=8080/tcp #重启防火墙(修改配置后要重启防火墙) firewall-cmd --reload # 参数解释 1、firwall-cmd:是Linux提供的操作firewall的一个工具; 2、--permanent:表示设置为持久; 3、--add-port:标识添加的端口;
六、nginx常用命令
cd /usr/local/nginx/sbin/ ./nginx 启动 ./nginx -s stop 停止 ./nginx -s quit 安全退出 ./nginx -s reload 重新加载配置文件 ps aux|grep nginx 查看nginx进程
七、配置负载均衡和两个项目的的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#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 - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$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;
upstream fzjun{
//负载均衡配置
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost; #域名的配置 www.xxx.com;
index index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
#第一个vue项目
location / {
root /usr/local/server/war/portal;
index index.html index.htm;
}
#第二个vue项目
location /ycfj {
alias /usr/local/server/war/ycfj/;#注意这里
index index.html index.htm;
try_files $uri $uri/ /ycfj/index.html; #解决页面刷新404问题
}
#后端项目 请求转发至8080端口
location /prod-api {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://你的ip地址:8080/prod-api;
}
}
}