nginx的安装配置
2016-07-08 14:35
471 查看
NGINX的安装配置
1、在线安装
yum install nginx (此处不详细讲解)2、离线安装(下载之后进行安装)
2.1先下载nginx文件(以1.8为例)下载地址 http://nginx.org/download/nginx-1.8.0.tar.gz
2.2 安装
2.2.1 安装目录 /usr/local/java cp nginx-1.8.0.tar.gz /usr/local/java
2.2.2 解压 tar -zxvf nginx-1.8.0.tar.gz
2.2.3 安装
cd nginx-1.8.0
./configure
make
make install
默认安装目录 /usr/local/nginx
启动nginx
cd sbin/
./nginx 报错[emerg] still could not bind()
nginx默认绑定80端口修改默认端口
cd ..
3.配置
vi conf/nginx.confnginx完整配置
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" '; //日志格式 #access_log logs/access.log main; //默认 主日志 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; upstream myServer{#.........Ip..... ip_hash;//用户集群,按照ip进行分发,保证同一个ip过来的请求分发到同一台服务器 session 不能与backup配合使用 server 127.0.0.1:7075 down;//down代表该台服务器不做集群 } server { listen 80; //nginx默认的端口是80端口 server_name m.****.com; // #charset koi8-r; access_log logs/m.log; //打印日志位置 (日志不会分时间,所有日志都打在这个文件里面,若是要一天一个日志文件,用shell命令然后定时切片) location / { proxy_pass http://localhost:7080/; //反向代理到这个端口 proxy_redirect off; proxy_set_header Host $host:$server_port; //非80端口时一定要加:$server_port 否则调转时直接到80端口 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 1; proxy_read_timeout 30; root /tomcat/tomcat-7080/webapps/m; proxy_set_header Cookie $http_cookie; proxy_cookie_path /m/ /; proxy_cookie_path /m /; index index.html index.htm; if ($request_uri = /){ //首次进入时进行分发 rewrite ^(.*)$ $1/m/index break; } } rewrite ^(.*)\;jsessionid=(.*)$ $1 break; #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 80; server_name cms.****.com; root /tomcat/tomcat-8080/webapps/cms; access_log logs/cms.log; location / { proxy_pass http://localhost:8080/; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header Remote_Addr $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded- be1f For $proxy_add_x_forwarded_for; proxy_connect_timeout 1; proxy_read_timeout 30; proxy_cookie_path /cms/ /; proxy_cookie_path /cms /; add_header From www.****.com; proxy_set_header Cookie $http_cookie; # rewrite ^(.*)$ http://cms.****.com/cms/login break; if ($request_uri = /){ rewrite ^(.*)$ http://cms.****.com/cms/login break; } } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #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; # } #}
}
然后再重新启动nginx OK 重启nginx 进入sbin目录 ./nginx -s reload
对nginx日志进行切片
shell文件
/#!/bin/bash //shell 文件格式,保存为sh文件
LOGS_PATH=/usr/local/nginx/logs //Nginx 日志文件所在的目录
YESTERDAY=(date−d“yesterday”+mv{LOGS_PATH}/access.log {LOGS_PATH}/access_{YESTERDAY}.log //移动文件
mv LOGSPATH/m.log{LOGS_PATH}/m_YESTERDAY.logmv{LOGS_PATH}/cms.log {LOGS_PATH}/cms_{YESTERDAY}.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid) // 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件
然后保存 赋予执行权限 chomd +x 文件名称
然后 vi /etc/crontab 里面添加定时任务
相关文章推荐
- Nginx 配置笔记
- Nginx Location配置总结
- Centos6.4上安装Nginx
- nginx防恶意域名解析
- Nginx使用手册目录
- 如何使用 Datadog 监控 NGINX(第三篇)
- 转载 Nginx的安装与使用
- Nginx 重定向所有子域名到www
- nginx经验
- nginx负载均衡配置。
- Nginx开启gzip
- FastDFS的配置、部署与API使用解读(7)Nginx的FastDFS模块
- Nginx防盗链详细解说
- nginx缓存设置
- Apache与Nginx的优缺点比较
- nginx ssl配置
- RTMP流媒体服务端应用开发系列 – Nginx-Rtmp分离音视频流<转>
- nginx负载均衡与主备设置
- nginx随记1
- nginx安装