第二十八章 DragonFlyBSD
第二节 Nginx

安装

  • pkg: # pkg install nginx
  • ports: # cd /usr/ports/www/nginx/ && make install clean

查找相关的软件包

  • ports: $ ls /usr/ports/www/ | grep nginx
  • pkg: $ pkg search -o nginx

配置

配置教程可参阅 官方文档官方维基
本文仅简单说明 FreeBSD 中如何启动 Nginx 及 Nginx 的配置文件。

启动

1
# sysrc nginx_enable=YES
2
# service nginx start
Copied!
你可以通过$ sockstat -4 | grep nginx检查 nginx 是否启动并正常运行。

配置文件

FreeBSD中,Nginx 的配置文件位于 /usr/local/etc/nginx/ 中,而主要的配置文件则在 /usr/local/etc/nginx/nginx.conf
默认配置中 Nginx 的根目录为 /usr/lccal/www/nginx/,如果需要更改目录位置,请将 /usr/local/etc/nginx/nginx.conf 中的
1
root /usr/lccal/www/nginx;
Copied!
改成你想要的目录位置,例如 root /path/to/new/webroot;

示例文件(Nginx + Typecho 伪静态 + SSL)

1
#user nobody;
2
worker_processes 1;
3
4
# This default error log path is compiled-in to make sure configuration parsing
5
# errors are logged somewhere, especially during unattended boot when stderr
6
# isn't normally logged anywhere. This path will be touched on every nginx
7
# start regardless of error log location configured here. See
8
# https://trac.nginx.org/nginx/ticket/147 for more info.
9
#
10
#error_log /var/log/nginx/error.log;
11
#
12
13
#pid logs/nginx.pid;
14
15
16
events {
17
worker_connections 1024;
18
}
19
20
21
http {
22
include mime.types;
23
default_type application/octet-stream;
24
25
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
26
# '$status $body_bytes_sent "$http_referer" '
27
# '"$http_user_agent" "$http_x_forwarded_for"';
28
29
#access_log logs/access.log main;
30
31
sendfile on;
32
#tcp_nopush on;
33
34
#keepalive_timeout 0;
35
keepalive_timeout 65;
36
37
#gzip on;
38
39
server {
40
listen 80;
41
server_name localhost;
42
43
#charset koi8-r;
44
45
#access_log logs/host.access.log main;
46
47
location / {
48
root /usr/local/www/nginx;
49
index index.html index.htm index.php;
50
if (-f $request_filename/index.html){
51
rewrite (.*) $1/index.html break;
52
}
53
if (-f $request_filename/index.php){
54
rewrite (.*) $1/index.php;
55
}
56
if (!-f $request_filename){
57
rewrite (.*) /index.php;}
58
}
59
60
61
location ~ .*.php(/.*)*$ {
62
root /usr/local/www/nginx;
63
fastcgi_pass 127.0.0.1:9000;
64
fastcgi_index index.php;
65
fastcgi_param SCRIPT_FILENAME $request_filename;
66
include fastcgi_params;
67
}
68
69
70
71
72
73
#location ~ .*.php(/.*)*$ {
74
# include fastcgi_params;
75
# fastcgi_pass 127.0.0.1:9000;
76
# }
77
78
#error_page 404 /404.html;
79
80
# redirect server error pages to the static page /50x.html
81
#
82
error_page 500 502 503 504 /50x.html;
83
location = /50x.html {
84
root /usr/local/www/nginx-dist;
85
}
86
87
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
88
#
89
#location ~ .php$ {
90
# proxy_pass http://127.0.0.1;
91
#}
92
93
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
94
#
95
location ~ .*.php(/.*)*$ {
96
root /usr/local/www/nginx;
97
fastcgi_pass 127.0.0.1:9000;
98
fastcgi_index index.php;
99
fastcgi_param SCRIPT_FILENAME $request_filename;
100
include fastcgi_params;
101
}
102
103
# deny access to .htaccess files, if Apache's document root
104
# concurs with nginx's one
105
#
106
#location ~ /.ht {
107
# deny all;
108
#}
109
}
110
111
112
# another virtual host using mix of IP-, name-, and port-based configuration
113
#
114
#server {
115
# listen 8000;
116
# listen somename:8080;
117
# server_name somename alias another.alias;
118
119
# location / {
120
# root html;
121
# index index.html index.htm;
122
# }
123
#}
124
125
126
# HTTPS server
127
#
128
server {
129
listen 443;
130
server_name localhost;
131
132
ssl_certificate /usr/local/etc/nginx/fbxs.crt;
133
ssl_certificate_key /usr/local/etc/nginx/fbxs.key;
134
135
ssl on;
136
ssl_certificate fbxs.crt;
137
ssl_certificate_key fbxs.key;
138
ssl_session_timeout 5m;
139
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
140
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
141
ssl_prefer_server_ciphers on;
142
location ~ .*.php(/.*)*$ {
143
root /usr/local/www/nginx-dist;
144
fastcgi_pass 127.0.0.1:9000;
145
fastcgi_index index.php;
146
fastcgi_param SCRIPT_FILENAME $request_filename;
147
include fastcgi_params;
148
}
149
location / {
150
root /usr/local/www/nginx-dist;
151
index index.php;
152
}
153
}
154
155
}
Copied!