nginx笔记
常用命令
which nginx #查看nginx执行路径,一般位于/usr/sbin/nginx #需要sudo来执行
sudo nginx -s reload #动态载入配置文件,不需要重启服务器,配置文件一般位于/etc/nginx/
sudo service nginx restart/stop/start
基本知识储备
常用配置指令
root #可以放在http、server或location上下文内部的任何层级,指定查询文件的根目录
autoindex on; #自动产生一个目录的文件列表,如果index指定的文件并不存在
index index.html; #默认索引文件。为了返回该文件,nginx会检查它是否存在并内部跳转到
#原url拼接index文件名所生成的新链接,内部跳转会重新查询location的匹配
try_files #检查指定文件或目录是否存在并做一个内部跳转,当不存在时,返回指定的状态码
正则表达式匹配操作符
~ #区分大小写 !~表示不匹配
~* #不区分大小写 !~*表示不匹配
一些配置示例
优化处理静态内容的配置
sendfile on; #默认情况下,nginx处理文件传输的方式是复制内容到缓冲区,然后再发送。启用
#sendfile后,减少了复制进缓冲区的步骤而直接从一个文件描述符复制数据到另一个。
sendfile_max_chunk 1m; #为了避免一个连接完全占用一个worker进程,可以限制每个单独的
#sendfile()系统调用所传输的数据量
tcp_nopush on; #和"sendfile on;"配置同时使用,使nginx在取得文件数据之后的同一个packet中
#同时发送响应的headers,也就是响应的headers和一个文件的起始部分在同一个
#packet中发送
tcp_nodelay on; #取消Nagle算法的0.2s默认等待延时,只在连接转变为keep-alive状态时启用
相关链接:
配置运行php
server {
listen 80;
server_name php.lutaoact.com;
root /usr/local/nginx/html/weiqing;
index index.php;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# php-fpm默认以www-data用户运行,需要修改nginx.conf使worker进程运行在www-data用户下
# user www-data
相关链接:
端口转发的配置
server
{
listen 80 default_server; #host无法匹配到任何server_name的请求,默认在这里处理
server_name lutaoact.com;
location / {
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:9000; #转发到9000端口
}
access_log /data/log/lutaoact.com.access.log;
}
处理CORS的配置
server
{
listen 80;
server_name lutaoact.com;
location / {
set $cors '';
if ($http_origin ~* '(localhost|192\.168\.1\.155)') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
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';
#add_header 'Access-Control-Allow-Headers' '*';
}
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
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';
#add_header 'Access-Control-Allow-Headers' '*';
return 204;
}
}
access_log /data/log/lutaoact.com.access.log;
}
注意事项
- 启动
nginx
时,若默认绑定到80
端口,则需要sudo
权限
安装
centos
- 建立
/etc/yum.repos.d/nginx.repo
文件,内容参考yum repo - 签名下载地址:nginx_signing.key,可使用
wget
或curl
下载 - 导入数字签名:
sudo rpm --import nginx_signing.key
sudo yum update && sudo yum install -y nginx
ubuntu
- 未完待续