NGINX完整全配置
之前的一些零碎的nginx配置,本文收集一下,基本上涉及所有配置项,用的时候删除不需要的即可;
server {
listen 80;
#listen 0.0.0.0:80;
#listen [::]:443;
#listen localhost:443;
#listen 0.0.0.0:443 ssl;
#ssl_certificate cert/com.example.www.pem;
#ssl_certificate_key cert/com.example.www.key;
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
server_name www.example.com example.com;
set $package 'com.example.www';
root /datas/website/$package/public;
index index.php index.html index.htm;
#access_log /datas/logs/nginx/access.$package.log;
error_log /datas/logs/nginx/error.$package.log;
##redirect server error pages to the static page /50x.html
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
##sub domain auto redirect to www
if ($host != 'www.example.com') {
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
## storage files redirect
location ~* ^/(stated|static|uploaded|generated)/.*$ {
root /datas/website/com.example.www/storage;
}
##cdn file rewrite proxy
#location /temps/ {
# proxy_pass http://cdn.example.com/temps/;
#}
##proxy api to an api server
#location /api/{
# proxy_pass https://api.example.com/;
#}
##deny version control files
#location ~ .*.(svn|git|cvs){deny all;}
## html no cache
#location ~.*\.(html)$ {
# add_header 'Cache-Control' 'private,no-store,no-cache,must-revalidate,proxy-revalidate';
#}
#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
# expires 7d;
#}
#location ~ .*\.(js|css)?$ {
# expires 12h;
#}
##pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
#fastcgi_param PHP_VALUE "open_basedir=./:$document_root/../:/tmp/:/var/lib/php/";
include fastcgi_params;
}
##framwork rewrite
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
#rewrite ^(.*)$ /index.php last;
}
}
}
如果涉及到http到https的跳转,需要用到:
server {
listen 222.222.222.222:80;
server_name www.example.com example.com;
#rewrite ^/(.*)$ https://www.example.com/ last;
return 301 https://www.example.com$request_uri;
}
部分配置项析译:
rewrite 指令
语法结构:rewrite regex replacement [flag]
语法说明:重写指令匹配URL正则表达式/正则表达式内容替换成/flag标记
flag取值:last匹配完成后继续匹配,break匹配完成不匹配后面规则,redirect返回302临时重定向,parmanent返回301永久重定向
一般的thinkphp或laravel或yii2都需要配置重写,如果资源不存在则自动通过rewrite交给index.php处理。
如:rewrite ^(.*)$ /index.php?s=/$1 last;