最近配置nginx遇到一些坑,因此将配置过程,都记录下来。首先,步骤1到7是配置过程,第8点是解决一些常见问题,第9点是使用nginx进行反向代理的配置;
1、下载nginx:
1.1 可能需要安装一些依赖:
- 更新源:sudo apt-get update
- 安装c++依赖库:sudo apt-get install build-essential
sudo apt-get install libtool
- 安装openssl依赖库: sudo apt-get install openssl
- 安装pcre依赖库: sudo apt-get install libpcre3 libpcre3-dev
- 安装zlib依赖库:sudo apt-get install zlib1g-dev
2、解压缩:
tar -zxvf nginx-1.14.2.tar.gz
3、进入解压后的文件:
cd nginx-1.14.2
4、配置
./configure --prefix=/usr/local/nginx
5、 编译安装
make && make install(需要获取root权限,或直接使用root账号)
6、如果没有报错:那么恭喜你,
启动 nginx:/usr/local/nginx/sbin/nginx
7、查看nginx的线程运行状态:
ps -ef | grep nginx
8、如果遇到问题:
8.1 Implicit fallthrough error
linux下编译nginx会使用gcc,查看gcc版本:gcc -dumpversion ;如果gcc版本号为7,那么源代码的switch-case块中如果忘了加上break,有可能会报错Implicit fallthrough error。
解决方案:
- 通过vim打开nginx-1.14.2文件夹下的objs/Makefile文件
vim objs/Makefile
- CFLAGS表示编译的可选参数,我们需要在这个参数后面加上
就可以忽略gcc7编译时switch-case中缺少break的错误。-Wno-implicit-fallthrough
9、反向代理配置
- 找到nginx的配置文件nginx.conf:如果都是按之前步骤进行安装的,那么可以通过下面代码访问配置文件
vim /usr/local/nginx/conf/nginx.conf #找到nginx的配置文件 - nginx中对于反向代理服务的配置:
server {
listen 80; #监听的端口
server_name manage.program.com; #监听的域名
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host; #头信息
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#location:请求映射规则,/ 代表映射一切请求路径
location / {
proxy_pass http://127.0.0.1:10000; #proxy_pass,代理转发。所有以manage.program.com和80端口
#访问的请求,都会被转发给http://127.0.0.1:10000处理
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}