基本设置

只允许本机访问

一般用于设置内部服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 8888;
server_name localhost;
root /www/vux/vux/my;
location / {
allow 127.0.0.1;
deny all;
try_files $uri $uri/ /index.php?$query_string;
autoindex on;
index index.html index.htm index.php;
include /etc/nginx/conf.d/php-fpm;
}
}

域名(端口)转发

一般用于前后端完全分离的设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name douniu.kouhuo.site;
location / {
index index.html index.htm index.php;
proxy_redirect off;
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_pass http://127.0.0.1:8080;
}
location /api {
#rewrite ^.+api/?(.*)$ /$1 break;
proxy_redirect off;
proxy_set_header Host $host; #要转发的host
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8888/; #设置要转发的ip(域名)端口
}
}

访问php动态资源(fastcgi)

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
server {
listen 80;
server_name local.ladmin.com local.ladmin.com;
root "D:/items/laravel/lesson-admin/public";
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
#autoindex on;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "POST, GET, OPTIONS";
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Allow-Headers "Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With";
if ($request_method = 'OPTIONS'){
return 204;
}
}
}

配置header头的最大字节数

1
2
3
4
5
6
7
server{
...
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
...
}

设置超时时间

1
2
3
4
5
6
7
server{
...
proxy_connect_timeout 360;
proxy_send_timeout 360;
proxy_read_timeout 360;
...
}