nginx 配置
Nginx作为web服务器的配置:
http { } # 由ngx_http_core_module 模块所引入。配置一个静态的web服务器。
http { uptream { } server { location URL { } location URL { } } server { } }
配置指令:
server {} 定义一个虚拟主机。
例:基于端口的虚拟主机
server { listen 8080; server_name www.zhuqiyang.com; root "/www/web1"; }
listen:
listen 127.0.0.1:8000; listen 127.0.0.1; listen 8000; listen *:8000; listen localhost:8000;
server_name:
1、先做精确匹配;www.scriptjc.com
2、左侧通配符匹配;*.scriptjc.com
3、右侧通配符匹配;www.scriptjc.com www.*
4、正则表达式匹配: ~^.*\.scriptjc\.com$
5、default_server;
6、定义多个域名用空格分割;
root path:
资源路径映射,请求的资源路径。
location:
根据请求的URI来匹配对应的规则。
server{ listen 80; # 定义多个域名的话用空格分格 server_name www.scriptjc.com m.scriptjc.com; location / { #这里的根目录是相对于里面的root而言的。 root "/vhosts/web1"; } location /images/ {#匹配到就从root定义的位置找,实际的地址为/vhosts/images/images root "/vhosts/images"; } location ~* \.php$ { fastcgi } }
优先级:
= 精确匹配
~ 正则表达式模式匹配,匹配时区分字符大小写
~* 正则表达式模式匹配,匹配时忽略字符大小写
^~ URI前半部分匹配,地址的左半部分不检查正则表达式
不带任何符号的URL
alias path:
用于location配置段,定义路径别名。路径映射
location /images/ {#这里的实际的地址为/vhosts/images/,也就是将长地址替换为短地址。
alias "/vhosts/images/"; 后面的斜线 / 不能省略,因为是别名。
}
默认主页:
格式:index file;例:index index.php index.html
错误页面:
格式:error_page code ... [=code] URI | @name;
根据错误码来指明特定的错误页面。
例:error_page 404 /404.html
[=code] 指定响应吗,当前页面响应吗为404,但是可以指定其响应吗,error_page 404 =200 /page.html。
基于IP访问控制:
allow IP/网段
deny IP/网段
deny all 拒绝所有。
基于用户访问控制:
basic,digest
auth_basic "any name";
auth_basic_user_file "user and passwd file path",使用htpasswd创建
htpasswd -c -m /etc/nginx/user/.htpasswd username,回车输入密码,这个路径就是上面的路径
https服务:
生成私钥,生成证书签署请求,并获得证书。
server { listen 443 ssl; server_name localhost; ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }
状态页:
stub_status on | off 显示nginx服务器状态信息的页面
location /status { stub_status on; allow 192.168.96.0/24; deny all; }
rewrite:
rewrite regex replacement flag;
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
flage:
last:被此规则匹配后忽略后面的规则,将重写后的地址重新交给nginx,再重新匹配。
break:直接请求重写后的地址。
redirect:临时重定向 302
permanent:永久重定向 301
例:
location { rewrite ^/bbs/(.*)$ /form/$1 break; }
if:
if condition { }, 应用于server location,condition可以使用
变量名,
=、
!=、
~区分大小写、
~*不区分大小写,
!~,!~*对上面两种取反,
-f文件、!-f不是文件
-d 目录 !-d
-e 、!-e是否存在
-x、!-x 是否有权限
例:客户端类型判断
if ($http_user_agent ~* MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
防盗链:
location ~* \.(jpg|gif|png)$ { valid_referer none blocked www.scriptjc.com; if () { rewrite ^/ http://www.scriptjc.com; } }
定制访问日志格式:
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;
此处可用nginx的内建变量
网络连接相关配置:
keepalive_timeout #;长连接超时时长。默认75s。
keepalive_requests #;请求的最大资源数。
keepalive_disable [mise6|safari|none]禁用长连接
tcp_nodelay on | off 是否对长连接使用TCP_NODELY选项
client_header_timeout #;读取http请求报文的超时时长
client_body_timeout #;读取http请求报文body部分的超时时长
send_timeout #;发送响应报文的超时时长
fastcgi ThinkPHP rewrite:
下面这个配置适用于ThinkPHP rewrite等三种模式都支持,可以直接拿来使用。要注意一点 配置中的 include fastcgi_params 这个fastcgi_params 是nginx安装好后默认的配置,无需修改,如果修改了将其改为如下的fastcgi_params 配置。
server { listen 80; server_name www.scriptjc.com; root /usr/share/nginx/html; index index.html index.htm index.php; error_page 404 /404.html; location = /404.html { return 404 'Sorry, File not Found!'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location / { try_files $uri @rewrite; } location @rewrite { set $static 0; if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) { set $static 1; } if ($static = 0) { rewrite ^/(.*)$ /index.php?s=/$1; } } location ~ /Uploads/.*\.php$ { deny all; } location ~ \.php/ { if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_NAME $1; fastcgi_param PATH_INFO $2; fastcgi_param SCRIPT_FILENAME $document_root$1; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
fastcgi_params 配置:这个配置是原有的默认配置,ThinkPHP的rewrite模式需要如下配置。
fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
所有页面显示到一个页面:
将网站上所有的页面都显示到指定的维护页面。
location / { rewrite ^/(.*)$ /index.html break; root /var/www/html; }
http强制跳转https:
upstream scan { server prd-nessus001 8834 max_fails=1 fail_timeout=15s; } # http强制跳转https server { listen 80; server_name scan.innodealing.com; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443 ssl; server_name scan.innodealing.com; ssl_certificate cert/innodealing.crt; ssl_certificate_key cert/innodealing.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; #ssl_ciphers HIGH:!aNULL:!MD5; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; access_log logs/access-https-scan.log main; location / { include conf.d/allow-swagger; include conf.d/addnocache; proxy_pass https://scan; include conf.d/proxy_params; } }