Nginx 配置

安装环境依赖

1
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel

下载Nginx包

1
2
3
mkdir /export/server  #创建存放安装包的文件
cd /export/server #进入文件中
wget http://nginx.org/download/nginx-1.16.1.tar.gz #通过wget命令下载Nginx包

编译Nginx

1
2
3
4
5
6
7
tar -zxvf nginx-1.16.1.tar.gz  #解压安装包 
#编译Nginx
./configure --with-http_ssl_module
./configure --with-stream
./configure --with-http_ssl_module --with-stream
./configure

安装Nginx

1
2
make && make install 

启动Nginx

1
2
3
cd /usr/local/nginx/sbin
./nginx #启动Nginx

配置Nginx

1
2
3
4
cd /usr/local/nginx/sbin  
./nginx -v #查看Nginx版本
./nginx -t #检查配置文件是否正确
./nginx -V #查看安装时配置

查看Nginx进程

1
2
ps -ef | grep nginx #查看Nginx进程
kill pad #杀死相关进程

Nginx常用命令

1
2
3
4
5
cd /usr/local/nginx/sbin  
./nginx -s reload #重新载入配置文件
./nginx -s reopen #重启Nginx
./nginx -s stop #停止Nginx

Nginx服务的主配置文件 nginx.conf

全局配置

1
2
3
4
5
#user nobody; 					#运行用户,若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量,可配置成服务器内核数 * 2
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置

I/O事件配置

1
2
3
4
5
6
7
8
events {
use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 1024; #每个进程处理 1024个连接 (可以修改)
}
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.

HTTP配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
http {
##文件扩展名与文件类型映射表
include 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 logs/access.log main;

##支持文件发送(下载)
sendfile on;
##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;

##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;

##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;

##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.ygy.com;

##网页的默认字符集
charset utf-8;

##根目录配置
location / {

##网站根目录的位置/usr/local/nginx/html
root html;

##默认首页文件名
index index.html index.htm;
}

##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
location = /50x.html {
root html;
}
}
}

基于IP和端口的访问控制

创建网页内容存放的地方

1
2
mkdir -p /opt/laochen/Objone
mkdir -p /opt/laochen/Objtwo

配置nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
server {
listen 8086;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /opt/laochen/Objone/views;
index index.html index.htm;
}

# 匹配url中带有api的,并转发
location /laochen_api {
# 利用正则进行匹配#去掉api前缀,$1是正则中的第一串,这样后端的接口也不需要带api了
#rewrite ^/laochen_api/(.*)$ /$1 break;
# 转发的参数设定
proxy_pass http://192.168.6.88:8085;
}

location /lc-autoqc {
alias /opt/laochen/lc-autoqc/views;
index index.html index.htm;
}

location /lc-autoplus {
alias /opt/laochen/lc-autoplus/views;
index index.html index.htm;
}
location /lc_api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 3600s;
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://localhost:8085/openapi-web-lc;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

修改hosts文件

1
vim  /etc/hosts