Ubuntu安装nginx

207 阅读1分钟

前言

nginx是一个高性能的http服务器,也可以用来做反向代理或者负载均衡等功能,一般在web开发过程中,基本都用到nginx作为代理

nginx安装

本文使用的是乌班图自带的nginx,安装教程如下

安装nginx

使用以下命令安装

apt install nginx

启动nginx

service nginx start

然后在系统中,使用以下命令

nginx -v

如果打印出版本号,那么证明安装成功了

image.png

nginx代理

一般简单代理的话,可以这么配置


server {  
listen 6023;  
root /usr/local/html/dist/;  
index index.html;  
  
location ~ ^/(api|download)/ {  
proxy_pass http://127.0.0.1:6022;  
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 Cookie $http_cookie;  
log_subrequest on;  
  
client_max_body_size 20m;  
#try_files $uri $uri/ =404;  
}  
}

使用6023端口访问静态资源,然后将api前缀的代理到后端接口,具体ip自己配置,也就是后端的服务器地址