卸载
apt-get remove --purge nginx
安装
sudo apt-get install nginx
nginx的使用
切换到nginx 的配置文件夹目录下
cd /etc/nginx/conf.d
这里我们需要添加对应网站的配置文件。这里给一个常用的命名规则:项目名+二级域名+端口.conf
开始编辑我们的conf文件 。vim ice-qjnubk-3000.conf ,复制以下代码进去
worker_processes 3; # 配置允许Nginx进程生成的worker process数 error_log logs/error.log; # 配置Nginx服务器运行时错误日志存放路径 pid nginx.pid; # 配置Nginx服务器运行时的pid文件存放路径和名称 events { use epoll; # 配置事件驱动模型 worker_connections 1024; # 配置最大连接数 } #### http块 开始 #### http{ include mime.types; #定义MIME-Type default_type application/octet-stream; sendfile on; #配置允许使用sendfile方式传输 keepalive_timeout 65; #配置连接超时时间 log_format access.log '$remote_addr-[$time_local]-"$request"-"$http_user_agent" '; #配置请求处理日志的格式 ## 配置虚拟主机 myServer1 server { listen 8082; #配置监听端口和主机名称(基于名称) server_name 0.0.0.0; access_log /nnt/nginx_log/access.log; #配置请求处理日志存放路径 location ^~ / { root /data01/cwl/alarm/dist; index index.html; try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; # 静态网页 } location ^~ /api { proxy_pass http://127.0.0.1:8005; } #error_page 500 502 503 504 /50x.html; #配置错误页 #location = /50x.html { # root html; #} #error_page 404 /404.html; #对错误页面404.html做了定向配置 #location = /404.html { # root /myweb/; # index 404.html; #} } } #### http块 结束 ####
保存退出
报错
注意以上配置想要运行成功的前提是/etc/nginx这个目录下没有nginx.conf,不然会一直报错。
如果/etc/nginx目录下有nginx.conf 那么自己新建的conf文件里面可以直接写server{…}
#### 有/etc/nginx/nginx.conf文件时 #### server { listen 9000; server_name localhost; client_max_body_size 5000m; client_body_buffer_size 5000m; keepalive_timeout 1200; client_body_timeout 1200; client_header_timeout 1200; send_timeout 1200; proxy_read_timeout 1200; proxy_send_timeout 1200; root /ntt/Works/iltres/ILTres/dist/; index index.html; location ~ .*\.(html)$ { add_header Cache-Control no-store; add_header Pragma no-cache; } location ^~ /api { proxy_pass http://127.0.0.1:8005; } }
重启
service nginx restart
查看报错
sudo nginx -t
全局变量
$remote_addr //获取客户端ip
$binary_remote_addr //客户端ip(二进制)
$remote_port //客户端port,如:50472
$remote_user //已经经过Auth Basic Module验证的用户名
$host //请求主机头字段,否则为服务器名称,如:blog.sakmon.com
$request //用户请求信息,如:GET ?a=1&b=2 HTTP/1.1
$request_filename //当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.html
$status //请求的响应状态码,如:200
$body_bytes_sent // 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40
$content_length // 等于请求行的“Content_Length”的值
$content_type // 等于请求行的“Content_Type”的值
$http_referer // 引用地址
$http_user_agent // 客户端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
$args //与$query_string相同 等于当中URL的参数(GET),如a=1&b=2
$document_uri //与$uri相同 这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$document_root //针对当前请求的根路径设置值
$hostname //如:centos53.localdomain
$http_cookie //客户端cookie信息
$cookie_COOKIE //cookie COOKIE变量的值
$is_args //如果有$args参数,这个变量等于”?”,否则等于”",空值,如?
$limit_rate //这个变量可以限制连接速率,0表示不限速
$query_string // 与$args相同 等于当中URL的参数(GET),如a=1&b=2
$request_body // 记录POST过来的数据信息
$request_body_file //客户端请求主体信息的临时文件名
$request_method //客户端请求的动作,通常为GET或POST,如:GET
$request_uri //包含请求参数的原始URI,不包含主机名,如:/2013/81.html?a=1&b=2
$scheme //HTTP方法(如http,https),如:http
$uri //这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$request_completion //如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK
$server_protocol //请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
$server_addr //服务器IP地址,在完成一次系统调用后可以确定这个值
$server_name //服务器名称,如:blog.sakmon.com
$server_port //请求到达服务器的端口号,如:80