nginx安装及基础配置(含jdk安装及配置)

时间:2023-11-21 22:01:08

0.jdk安装配置

    #下载相应的jdk软件包,然后解压安装,我这里包名称为:jdk-7u25-linux-x64.tar.gz   

    tar -xzf  jdk-7u25-linux-x64.tar.gz  
   mkdir -p /usr/java/
   mv jdk1.7.0_25/ /usr/java/ #然后配置环境变量,这样可以任何地方引用jdk,如下配置: #vi /etc/profile 最后面加入以下语句: export JAVA_HOME=/usr/java/jdk1.7.0_25 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin #source /etc/profile #使环境变量马上生效 #java --version #查看java版本,看到jdk1.7.0_25版本即代表java jdk安装成功。

1.官网

http://nginx.org/

2.nginx安装

2.1cd /user/local/software目录下(没有的话,就创建)

2.2上传nginx包及高可用包到该目录下

nginx-1.6.2.tar.gz

keepalived-1.2.18.tar.gz

nginx安装及基础配置(含jdk安装及配置)

2.3解压至 /usr/local下

tar -zxf nginx-1.6.2.tar.gz /usr/local

2.4下载锁需要的依赖库文件:

        yum install pcre

        yum install pcre-devel

        yum install zlib

        yum install zlib-devel

2.5进行configure配置:

cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx  

下图代表成功

nginx安装及基础配置(含jdk安装及配置)

查看是否报错:

报错1:error: C compiler cc is not found

解决方案:

安装gcc-c++包

命令:yum -y install gcc-c++

-y:所有的询问均选择yes

 2.6编译安装(cd 到 /usr/local/nginx-1.6.2目录下)

make && make install

2.7启动nginx

cd /usr/local/nginx目录下: 看到如下4个目录
....conf 配置文件
... html 网页文件
...logs 日志文件
...sbin 主要二进制程序

启动命令:(/usr/local/nginx/sbin/nginx -s start)

/usr/local/nginx/sbin/nginx

查看是否启动成功:(netstat -ano | grep 80)或者如下:

ps -ef|grep nginx

nginx安装及基础配置(含jdk安装及配置)

关闭命令:(/usr/local/nginx/sbin/nginx -s stop)

/usr/local/nginx/sbin/nginx -s stop

重启命令:(/usr/local/nginx/sbin/nginx -s reload)

/usr/local/nginx/sbin/nginx -s reload

浏览器访问地址:(看到欢迎页面即可)

http://192.168.0.100:80

nginx安装及基础配置(含jdk安装及配置)

若是失败:可能为80端口被占用等。

lsof -i:80查看进程pid,然后kill -9 pid即可

3.nginx虚拟主机配置(即配置nginx.conf文件)

3.1假设将nginx.conf中的80端口改为8081

nginx安装及基础配置(含jdk安装及配置)

cd 到/etc/sysconfig/下,找到iptables文件,添加如下一行,端口放行记录

nginx安装及基础配置(含jdk安装及配置)

保存退出后(centos7执行下述命令)
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
3.2nginx.conf配置说明
#user  nobody;

#开启进程数 <=CPU数 
worker_processes 1; #错误日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #进程号保存文件
#pid logs/nginx.pid; #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
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"'; #请求日志保存位置(这里是全局的配置,适用于所有server)
#access_log logs/access.log main; #打开发送文件
sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
#连接超时时间
keepalive_timeout 65; #打开gzip压缩
#gzip on; #设定请求缓冲
#client_header_buffer_size 1k;
#large_client_header_buffers 4 4k; #设定负载均衡的服务器列表
#upstream myproject {
#weigth参数表示权值,权值越高被分配到的几率越大
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#} #webapp
#upstream myapp {
# server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;
# server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;
#} #配置虚拟主机,基于域名、ip和端口
server {
#监听端口
listen 80;
#监听域名
server_name localhost; #charset koi8-r; #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
#access_log logs/host.access.log main; #返回的相应文件地址
location / {
#设置客户端真实ip地址
#proxy_set_header X-real-ip $remote_addr;
#负载均衡反向代理
#proxy_pass http://myapp;

#返回根路径地址(相对路径:相对于/usr/local/nginx/

root html;
#默认访问文件
index index.html index.htm;
} #配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
#location ~ \.jsp$ {
# proxy_pass http://192.168.1.171:8080;#此处是tomcat所在服务器的ip:端口
#} #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 1234;
server_name bhz.com;
location / {
#正则表达式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一个test123.html 然后使用正则匹配
#location ~ test {
## 重写语法:if return (条件 = ~ ~*)
#if ($remote_addr = 192.168.1.200) {
# return 401;
#} #if ($http_user_agent ~* firefox) {
# rewrite ^.*$ /firefox.html;
# break;
#} root bhz.com;
index index.html;
} #location /goods {
# rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
# root bhz.com;
# index index.html;
#} #配置访问日志(这里是局部的配置,适用于该server)
access_log logs/bhz.com.access.log main;
} # 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;
# }
#} }

 3.3location拦截说明:

3.3.1将指定IP加入黑名单,告知其鉴权失败(注意空格!!!!!!!!!!!!!)

nginx安装及基础配置(含jdk安装及配置)

192.168.0.103访问时,如下结果

nginx安装及基础配置(含jdk安装及配置)

3.3.2 拦截user_agent含有firefox的请求

nginx安装及基础配置(含jdk安装及配置)

3.3.3实现动静分离
3.3.3.1如果是请求的是某个url,静态网页,则走其中一个location

3.3.3.2如果是访问的是另一个url,动态网页,如jsp,php,则可以通过反向代理,转发给apache或者tomcat,走另一个location

基础语法:

location = {}#精准匹配

nginx安装及基础配置(含jdk安装及配置)

location {}#一般匹配

nginx安装及基础配置(含jdk安装及配置)

location ~{}#正则匹配

nginx安装及基础配置(含jdk安装及配置)

nginx安装及基础配置(含jdk安装及配置)

 location [=|~|~*|^~] /uri/ { … }
  符号    含义
  =      开头表示精确匹配
  ^~     开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
  ~      开头表示区分大小写的正则匹配
  ~*     开头表示不区分大小写的正则匹配
  !~和!~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
  /      通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =
其次匹配 ^~
其次是按文件中顺序的正则匹配
最后是交给 / 通用匹配
当有匹配成功时候,停止匹配,按当前匹配规则处理请求
例子,有如下匹配规则:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}
ReWrite语法

last – 基本上都用这个Flag
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
Redirect语法 server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn$&quot {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
防盗链

location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
根据文件类型设置过期时间

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
禁止访问某个目录

location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

4.关键模块配置

4.1静态页面拦截规则  location(在server里)

4.2反向代理      proxy_pass(在location里)

4.3负载均衡      upstream(在server外)