Nginx简单配置

nginx简单配置及简单说明

nginx安装就不说了,直接上配置文件,改改就可以了:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

user nginx;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 2000000;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 2000000;
use epoll;
multi_accept on;
}


http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$http_x_forwarded_for '
'$upstream_addr '
'ups_resp_time: $upstream_response_time '
'request_time: $request_time'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 5000m;
client_body_buffer_size 512k;
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
proxy_buffer_size 512k;
proxy_buffers 8 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
fastcgi_read_timeout 3000;
fastcgi_buffers 8 5125;
send_timeout 3000;



sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_disable none;
keepalive_timeout 120;
keepalive_requests 65535;
types_hash_max_size 2048;

charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
reset_timedout_connection on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/json;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

include conf.d/*.conf;


}

APP.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
upstream apitest{
# ip_hash;
# least_conn;
server 192.168.0.1:8080;
server 192.168.0.2:8080;
server 192.168.0.3:8080;
}


server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root html;
# index index.html index.htm;
# proxy_pass http://apitest;
#}

location ^~ /api/ {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Connection "";
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header REMOTE-HOST $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://apitest/api/;
proxy_redirect off;
}

location ^~ /auth/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://apitest/auth/;
proxy_redirect off;
}

#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;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

简单说明

负载均衡

配置负载均衡,只要就是使用了节点upstream,参考格式如下:

1
2
3
4
5
6
#配置多台服务器(这里只在一台服务器上的不同端口)
upstream mysvr {
    #权重比设置为1 : 3 意思为 用户请求四次服务器,平均访问8082端口3次,8081端口一次,以此达到服务器均衡的作用(高并发)
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=3;
}

反向代理

反向代理只要就是用来将请求发送给其他主机的ap服务器,简单来说就是,nginx收到请求后,将请求转发给对应的ap服务器。例如:

1
2
3
location /apitest/ {
proxy_pass http://192.168.0.1:8080;
}

当请求收到以apitest/开头的请求,就会转发到http://192.168.0.1:8080;所以如果有个请求是http://192.168.3.2/apitest/getusername,通过nginx就会转到http://192.168.0.1:8080/apitest/getusername达到转发的效果,配合之前的负载均衡,就可以实现请求一个ip的接口,转到upstream中定义的多个ip的接口。

proxy_pass末尾“/”问题

在nginx配置proxy_pass代理转发时,如果proxy_pass后面的URL加了/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也代理走。

假设都访问http://192.168.2.1:8082/apitest/login.html

第一种

1
2
3
location /apitest/ {
proxy_pass http://127.0.0.1/;
}

代理到URL: http://127.0.0.1/apitest/login.html

第二种

1
2
3
location /apitest/ {
proxy_pass http://127.0.0.1;
}

第二种相对于第一种,proxy_pass后面的url少了/,代理到URL:http://127.0.0.1/apitest/login.html

第三种

1
2
3
location /apitest/ {
proxy_pass http://127.0.0.1/aaa/;
}

第三种相对于第一种,proxy_pass后面的url加了一层aaa/,代理到URL:http://127.0.0.1/aaa/login.html

第四种

1
2
3
location /apitest/ {
proxy_pass http://127.0.0.1/aaa;
}

第四种相对于第一种,proxy_pass后面的url加了一层aaa,代理到URL:http://127.0.0.1/aaalogin.html

完~

文章目录
  1. nginx.conf
  2. APP.conf
  3. 简单说明
    1. 负载均衡
    2. 反向代理
      1. proxy_pass末尾“/”问题
|