一、配置yum源
cd /etc/yum.repos.d/
touch ./nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck=0
enabled=1
二、安装
yum install nginx
三、修改nginx配置文件
例:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024; #最大连接数
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65; #连接超时时间
#gzip on;
#include /etc/nginx/conf.d/*.conf;
/# 负载均衡配置 weight 权重
upstream beijingList {
server 192.168.1.1:82 weight=3;
server 192.168.1.2:82 weight=2;
server 192.168.1.3:82 weight=1;
}
# linux
server {
listen 8081;
server_name 172.0.0.1 www.ceshi.cn;
location / {
root /usr/share/nginx/html/dist;
index index index.php index.html index.htm default.php default.htm default.html;
try_files $uri $uri/ /index.html;
}
location /admin/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.0.0.1:8888/admin/;
}
location /beijing {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://beijingList;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# win
server {
listen 8126; # 监听端口
server_name 172.0.0.1 www.ceshi.cn; # 监听地址 多个可用空格隔开
charset utf-8;
location / {
alias D:/Project/project1/vue/; # 前端项目地址
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location ~ (/rest/|/admin/|/login/|/demo/){
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8125;
}
}
}