安装
最简单的安装方式:
(Ubuntu)apt-get install nginx -y
(centos)yum install -y nginx
目录说明
可以通过whereis nginx查找存放nginx分别在哪里
其中比较重要的:
- /usr/sbin/nginx 执行目录(可在任何地方直接用nginx运行)
- /etc/nginx 配置文件所在目录(nginx.conf是默认配置文件)
相关命令
- nginx 启动nginx
- nginx -s stop 停止nginx
- nginx -s reload 重启nginx
- nginx -c 配置文件路径 使用指定配置文件启动nginx (可用它尝试解决一些报错,例如:
nginx:[error] open() "/run/nginx.pid" failed (2: No such file or directory)
- nginx -t 检查配置文件是否有问题
- netstat -antp | grep : 查看所有端口占用情况
- killall -9 nginx 杀掉nginx进程
- lsof -i:80 查看80端口的使用进程
配置文件nginx.conf
首先大坑,坑我到天亮:对nginx的配置主要写在nginx.conf文件里,这个目录下还有conf.d和sites-enabled两个文件夹,里面为默认的配置文件。相应的,在配置nginx,编辑nginx.conf文件时,需要把这两行注释掉,否则nginx.conf不会生效。
必须将http块里的这两行注释掉
在http块中加入如下文件,可启动反向代理
upstream yourname {
#需要被代理的地址
server 127.0.0.1:8090;
}
server {
listen 80;
listen [::]:80;
#域名
server_name www.yourdomain.com;
client_max_body_size 1024m;
location / {
proxy_pass http://yourname;#被代理的模块名字
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
简易流程
- 在linux上安装nginx
- 修改配置文件(注意删掉那两行该死的注释)
- 启动nginx