安装
根据系统来安装
$ sudo yum install epel-release && yum install nginx [On CentOS/RHEL]
$ sudo dnf install nginx [On Debian/Ubuntu]
$ sudo apt install nginx [On Fedora]
常用命令
- 检测配置文件:sudo nginx -t
- 启动:sudo service nginx start
- 重启:sudo service nginx restart
- 查看版本:nginx -v或则nginx -V
- 开机自启动:sudo service nginx enable
- 查看服务状态:sudo service nginx status
- 重新加载配置文件:sudo service nginx reload
- 停止服务:sudo service nginx stop
- 查看帮助命令:systemctl -h nginx
配置案例
场景一:设置代理
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
/***这里是配置内容-start***/
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://165.168.0.70:8000;
proxy_redirect default ;
}
/***这里是配置内容-end***/
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
场景二:如果地址访问不到则跳转首页
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri /index.html; //这里是配置内容
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://165.168.0.70:8000;
proxy_redirect default ;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}