nginx压缩
即可以对页面进行gzip压缩,然后传到用户那里,再解压;
实现方案即在nginx.conf的http块中增加如下配置:
gzip on;
gzip_min_length 1k; #最小1K
gzip_buffers 16 64K;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
压缩前
压缩后
即增加了Content-Encoding:gzip
nginx中缓存的使用
有很多人访问的网站来说,缓存肯定是很重要的东西了。一开始是想通过插件,让Nginx和Redis进行合成,然后Nginx使用Redis来缓存的,但是发现配置起来很麻烦,还要自己下载插件,重新编译Nginx,比较麻烦,所以这里觉得用Nginx自带的缓存也是不错的选择。虽然效率比不上redis,但是有还是比没有好。Nginx默认的缓存是磁盘文件系统的缓存,而不是像Redis那样的内存级别的缓存。一开始我以为Nginx就只有这样。后来查了资料,才知道是我太天真了,对Linux不是很了解导致的。Linux的一切皆文件。原来我们可以把文件缓存到内存对应的Linux文件系统中。我说的可能比较难以理解,请自行搜索/dev/shm 这个文件目录。我们把文件缓存到这个文件目录里,其实就相当与内存的缓存了。只不过还是靠文件系统管理。所以比不上自定义格式的Redis那样的内存缓存。在http段进行基本配置
#缓存配置
proxy_cache_key '$host:$server_port$request_uri';
proxy_temp_file_write_size 64k;
proxy_temp_path /users/mazhen/nginx/proxy_temp_path;
proxy_cache_path /users/mazhen/nginx/proxy_catche_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
proxy_cache cache_one;
proxy_cache_valid 200 304 302 5d;
proxy_cache_valid any 5d;
proxy_cache_key '$host:$server_port$request_uri';
add_header X-Cache '$upstream_cache_status from $host';
proxy_pass http://172.16.25.44:8082;
expires 30d; #缓存30天
}
这些不缓存的话,就要加上proxy_ignore_headers的配置项了。还有一点就是/dev/shm下面的文件系统权限默认只给root用户,所以要chmod 777 -R /dev/shm 这样不是很安全的做法,如果实际上线可以给定某个用户组,关于用户组的设置是配置的第一行
user www www;
上面第二段代码的第6行是增加一个header字段方便查看是否击中缓存。
Nginx增加SSL功能
同样的Nginx默认是有SSL模块功能,我们不用额外安装,只需要简单的配置就可以了。首先我们先来生成一些必要的证书。制作的过程还是比较简单的。
#制作CA证书
openssl genrsa -des3 -out ca.key 2048
openssl req -new -x509 -days 7305 -key ca.key -out ca.crt
#生成Nginx服务器所需证书,并使用CA签名
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
openssl x509 -req -in client.csr -out client.pem -signkey client.key -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650
#取消证书密码
openssl rsa -in client.key -out client.key.unsecure
下面就是配置Nginx了,我们可以把需要用到的client.pem, client.pem,client.key.unsecure这三个文件放到Nginx所在机器的一个目录下,如/users/mazhen/nginx,剩下的Nginx配置如下:
server{
server_name localhost;
listen 8081 ssl;
root html;
location / {
index index.html index.html;
}
#ssl on 的写法已经弃用
#ssl on;
ssl_certificate /users/mazhen/nginx/client.pem;
ssl_certificate_key /users/mazhen/nginx/client.key.unsecure;
}
重启Nginx,我们就可以访问Https网站了。 但是他出现这个:
提示您的链接不是私密连接,高级,继续访问即可。
这个是没有什么问题,具体原因是这个CA证书要得到认可。所以我们上面自己生成的https证书,只是自己生成的,如果要变成不提示安全问题的那种,就需要花钱购买了,剩下的这个自己上网解决。(虽然自己生成的证书可以用,但是还是抵挡不了DNS欺骗,所以这种不安全证书,跟没有其实是一样的。不过据说这样可以阻止运营商劫持。)
输入http连接时自动跳转到的https连接
在server中增加如下内容即可
新增一个server,用于接收localhost:8081,并且替换为https://localhost:8081
第一种写法:
server{
server_name localhost;
listen 8081 ;
rewrite ^(.*)$ https://$host$1 permanent;
}
测试结果:
nginx: [emerg] no "ssl_certificate" is defined for the "listen ... ssl" directive in /usr/local/etc/nginx/nginx.conf:50,明明配置了,但是报了这个错
解决方案:将 ssl_certificate /users/mazhen/nginx/client.pem;
ssl_certificate_key /users/mazhen/nginx/client.key.unsecure;
放到http部分.然后再次启动,提示nginx: [warn] conflicting server name "localhost" on 0.0.0.0:8081, ignored
所以将第二个server中的localhost修改为172.16.25.44,再次重启上面的错误提示没了。
问题:按照上面写法,浏览器中输入http://localhost:8081/rsbi,enter键后,会替换成https://localhost/rsbi,
即http替换成了https,但是端口8081丢了,奇怪
查到一个帖子,可能是host配置的不对,缺少了
proxy_set_header Host $host:$server_port;或者是配置错误,如proxy_set_header Host $host
即 Nginx没有正确的吧端口信息传送到后端,没能正确的配置nginx。
修改后发现依旧不行,仍就是将http://localhost:8081/rsbi/替换成了https://localhost/rsbi/,缺少了端口8081
----花了太多时间 这个问题暂时放放吧 有知道原因或修改方案的麻烦指教下。
现在的nginx.conf如下:
worker_processes 2;
pid /usr/local/var/run/nginx.pid;
#worker_rlimit_nofile 65535;
events{
#use epoll;
worker_connections 65535;
}
http{
include mime.types;
types_hash_max_size 2048;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
ssl_certificate /users/mazhen/nginx/client.pem;
ssl_certificate_key /users/mazhen/nginx/client.key.unsecure;
proxy_set_header Host $host:$server_port;
#强制将http转化为https请求,无效???
server{
server_name localhost;
listen 8081 ;
rewrite ^(.*)$ https://$host$1 permanent;
}
server{
listen 8081 ssl;
server_name 172.16.25.44 ;
#rewrite ^(.*)$ https://$host$1 permanent;
location / {
index index.jsp index.html index.htm index.php;
}
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
proxy_pass http://localhost:8082;
expires 30d;
}
location ~ .*$ {
index index;
proxy_pass http://172.16.25.44:8080;
}
}
#静态文件 server
server{
listen 8082;
server_name localhost;
location / {
}
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
#所有静态文件直接读取硬盘
root /users/dev/apache-tomcat-8.5.33/webapps/;
expires 30d; #缓存30天
}
}
}
完整的nginx.conf的配置实例和解释
# 定义Nginx运行的用户 和 用户组 如果对应服务器暴露在外面的话建议使用权限较小的用户 防止被入侵
# user www www;
#Nginx进程数, 建议设置为等于CPU总核心数
worker_processes 8;
#开启全局错误日志类型
error_log /var/log/nginx/error.log info;
#进程文件
pid /usr/local/var/run/nginx.pid;
#一个Nginx进程打开的最多文件描述数目 建议与ulimit -n一致
#如果面对高并发时 注意修改该值 ulimit -n 还有部分系统参数 而并非这个单独确定
worker_rlimit_nofile 65535;
events{
#使用epoll模型提高性能
#use epoll;
#单个进程最大连接数
worker_connections 65535;
}
http{
#扩展名与文件类型映射表
include mime.types;
#默认类型
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
#日志
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
#gzip 压缩传输
gzip on;
gzip_min_length 1k; #最小1K
gzip_buffers 16 64K;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
#负载均衡组
#静态服务器组
upstream static.zh-jieli.com {
server 127.0.0.1:808 weight=1;
}
#动态服务器组
upstream zh-jieli.com {
server 127.0.0.1:8080;
#server 172.16.25.44:8080;
}
#配置代理参数
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 65;
proxy_send_timeout 65;
proxy_read_timeout 65;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
#缓存配置
proxy_cache_key '$host:$server_port$request_uri';
proxy_temp_file_write_size 64k;
proxy_temp_path /dev/shm/JieLiERP/proxy_temp_path;
proxy_cache_path /dev/shm/JieLiERP/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
server{
listen 80;
server_name erp.zh-jieli.com;
location / {
index index; #默认主页为 /index
#proxy_pass http://jieli;
}
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
proxy_cache cache_one;
proxy_cache_valid 200 304 302 5d;
proxy_cache_valid any 5d;
proxy_cache_key '$host:$server_port$request_uri';
add_header X-Cache '$upstream_cache_status from $host';
proxy_pass http://static.zh-jieli.com;
#所有静态文件直接读取硬盘
# root /var/lib/tomcat7/webapps/JieLiERP/WEB-INF ;
expires 30d; #缓存30天
}
#其他页面反向代理到tomcat容器
location ~ .*$ {
index index;
proxy_pass http://zh-jieli.com;
}
}
server{
listen 808;
server_name static;
location / {
}
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
#所有静态文件直接读取硬盘
root /var/lib/tomcat7/webapps/JieLiERP/WEB-INF ;
expires 30d; #缓存30天
}
}
}
借鉴:
nginx+tomcat:https://www.cnblogs.com/wunaozai/p/5001742.html