Nginx

安装

yum方式安装

编辑 /etc/yum.repos.d/nginx.repo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[nginx-stable]
name=nginx stable repo
baseurl=http://mirrors.ustc.edu.cn/nginx/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://mirrors.ustc.edu.cn/nginx/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

安装

1
2
yum -y install yum-utils
yum install -y nginx

RPM方式安装

1
2
3
4
5
6
cd ~/download
wget https://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm

sudo yum -y localinstall nginx-1.20.2-1.el7.ngx.x86_64.rpm
rpm -qa |grep nginx
rpm -ql nginx

源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel

依赖:pcre, zlib

wget https://nginx.org/download/nginx-1.12.2.tar.gz
tar xzvf nginx-1.12.2.tar.gz
cd nginx-1.12.2

./configure \
--prefix=/usr/local/nginx/nginx1.12 \
--with-pcre=../pcre-8.40 \
--with-zlib=../zlib-1.2.11 \
--with-http_ssl_module

make && make install

运行

查看版本

1
nginx -V

日志统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
根据访问IP统计UV
awk '{print $1}' access.log|sort | uniq -c |wc -l

统计访问URL统计PV
awk '{print $7}' access.log|wc -l

查询访问最频繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more

查询访问最频繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more

根据时间段统计查看日志
cat access.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more

配置

纯文本服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
user  sharefile;
http {
charset UTF-8;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# autoindex on; 不建议开启

server {
listen 80;
server_name localhost;
rewrite_log on;

root /home/sharefile;
index index.html;

location / {
# 启用账号认证
# auth_basic "auth";
# auth_basic_user_file passwd/default;
}

location ~ \.txt {
add_header Content-Type text/plain;
}
}
}

配合Tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
server_name localhost;

proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;

location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /upload/ {
root /home/deploy/;
}
}

PHP 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name example.org www.example.org;
root /data/www;

location / {
index index.html index.php;
}

location ~* \.(gif|jpg|png)$ {
expires 30d;
}

location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}

proxy 服务

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name app1.xiongjiaxuan.com;

location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

负载服务

1
2
3
4
5
6
7
8
9
10
11
12
13
upstream appsite {
server 192.168.137.110:8080;
server 192.168.137.111:8080;
}

server {
listen 80;
server_name app1.xiongjiaxuan.com;

location / {
proxy_pass http://appsite/;
}
}

Virtual Hosts

1
2
3
4
http {
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
}

编辑 sites-enabled/blog.xiongjiaxuan.com.conf

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name blog.xiongjiaxuan.com;
access_log /var/log/nginx/blog-access.log;
error_log /var/log/nginx/blog-error.log;

root /webapp/blog/public;

location / {
index index.html;
}
}

安全

禁止通过IP访问

1
2
3
4
5
server {
listen 80 default_server;
server_name x;
return 404;
}

异常排查

413 Request Entity Too Large

加大提交数据大小

1
client_max_body_size 100m;

参考

使用iText处理PDF

  • iText was written by Bruno Lowagie.
  • 2007年,《SOA World》(SOA世界)杂志将iText作为企业应该使用的10种开源解决方案之一。詹姆斯·高斯林称赞iText库,并在Huckster的新版本使用。纽约时报还利用iText来创建其公共领域文章的PDF版本。
  • iText被嵌入在许多产品和服务中,包括Eclipse BIRT、Jasper Reports、Red Hat JBoss Seam、Windward Reports、pdftk等等。
  • 2008年末,iText的专有许可证可用;2009年初,iText软件公司成立,成为iText产品的全球许可方。

HTML转PDF

加密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.xiongjiaxuan;

import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;

import java.io.FileOutputStream;
import java.io.IOException;

/**
* Created by xiongjiaxuan on 2022/1/12 18:25
*/
public class Password {

public static final String SRC = "./src/main/resources/总表.pdf";
public static final String DEST = "./target/output/password.pdf";

private static final byte[] USERPASS = "user".getBytes(); // 可读
private static final byte[] OWNERPASS = "owner".getBytes(); // 可读写

public static void main(String[] args) throws IOException
{
PdfReader pdfReader = new PdfReader(SRC);
WriterProperties writerProperties = new WriterProperties();
writerProperties.setStandardEncryption(USERPASS, OWNERPASS, EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.ENCRYPTION_AES_128);
PdfWriter pdfWriter = new PdfWriter(new FileOutputStream(DEST), writerProperties);
PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter);
pdfDocument.close();
}

}

依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.9</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.69</version>
</dependency>

参考

顺时针旋转90度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.xiongjiaxuan;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;

/**
* Created by xiongjiaxuan on 2022/1/12 17:29
*/
public class Rotate {

public static final String DEST = "./target/output/rotate90.pdf";
public static final String SRC = "./src/main/resources/总表.pdf";

public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();

PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
PdfPage page = pdfDoc.getPage(1);
int rotate = page.getRotation();
if (rotate == 0) {
page.setRotation(90);
} else { // 存在角度
page.setRotation((rotate + 90) % 360);
}
pdfDoc.close();
}

}

依赖

1
2
3
4
5
6
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.9</version>
<type>pom</type>
</dependency>

参考

References

Bruno Lowagie - Stack Exchange

网页内容另存为PDF

目的

覆盖原网站的浏览器打印CSS,仅保留文章内容,使打印输出的PDF更友好。

步骤

1. Chrome浏览器开启打印预览模式
2. 找到样式文件
3. 追加覆盖样式
4. 右键打印保存为PDF

注意:如果图片作为背景图,需要勾选Background graphics

联合早报

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
header, .bg-gray, .breadcrumb,
.d-lg-block *,
div.col-lg-4:nth-child(3),
#video-info,
.article-relative-word,
#outbrain-wrapper,
footer,
#back-top-bar *,
.pdb20 > div:nth-child(2),
.further-reading,
body > div.container > div:nth-child(1),
body > div.container > div:nth-child(2) > div.col-lg-7.col-12 > div:nth-child(2) > div div
{
display: none;
}

a {
text-decoration: none!important;
}
.col-lg-7 {
max-width: 100% !important;
flex: auto;
}
.carousel-item img{
width: 100% !important;
height: 100% !important;
}
.f14 {
font-size: 20px;
}
body {
padding-top: 30px;
padding-bottom: 10px;
}
article {
font-size: 22px !important;
}
.author-info {
font-size: 20px !important;
}
.article-title {
font-size: 36px !important;
}
@page{size:a4}

BBC

News

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#main-content>div:not(.ssrcss-1ocoo3l-Wrap),
.ssrcss-hhhbpo-ContainerWithSidebarWrapper>div:not(.ssrcss-rgov1k-MainColumn),
.ssrcss-rgov1k-MainColumn>div,
.ssrcss-2z3pjz-SectionWrapper,
footer,
.e1nh2i2l0,
.teads-inread,
#header-content {
display: none !important;
}

/* 使图片不被截断 */
.ssrcss-wpgbih-StyledFigure {
page-break-inside: avoid;
}

.ssrcss-uf6wea-RichTextComponentWrapper {
max-width: none;
}

Worklife, Future, Culture

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#orb-banner,
#ad-portal,
.StickyElement,
.article-share-tools,
.article__similar-articles,
.teads-inread,
.article-body-native-ad,
.around-the-bbc-swimlane,
.article-end__share-tools,
.orb-footer,
.article-body__image-text,
.hero-image,
.article-headline__collection
{
display: none !important
}

.article__body {
max-width: none !important
}
.article__intro {
font-size: 32px
}
.body-text-card {
font-size: 12px
}
.simple-p-tag--medium {
font-size: 14px
}
.article-body__pull-quote {
margin: 10px 0;
}

BBC News 中文

  • 图片默认会被截断
  • 文字会截断
  • 正文中的推荐链接没有唯一标识,需要逐个编辑删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.bbc-1fe3qkx, /* 导航菜单 */
.bbc-1dklj5e, /* 顶部广告 */
.bbc-18r23py, /* 简繁切换按钮 */
.bbc-fm4knk, /* 【右侧】推荐内容 */
.bbc-113e14a, /* 【正文】广告 */
.bbc-1km06py, /* 【底部】相关主体 */
.bbc-1u4iszl, /* 【底部】推荐内容 */
.bbc-1s1cxbv, /* 图片上的标识 */
footer {
display: none;
}

/* 使图片不被截断 */
.bbc-1qdcvv9 {
page-break-inside: avoid;
}

/* 可能的推荐链接 */
.e1gggypo0.bbc-oa9drk.e57qer20 {
display: none;
}

/* 字体 */
.bbc-mj7obe {
font-size: 12px;
padding-bottom: 8px;
line-height: 1.5;
}
.bbc-6w9yzd {
font-size: 20px;
padding: 8px 0;
}
.bbc-6yjyco {
font-size: 28px;
line-height: 1.5;
padding: 10px 0;
}
.bbc-q4ibpr {
padding-bottom: 0;
}

/* logo */
.bbc-fk60et {
background: white;
}
.bbc-fk60et svg {
color: black;
}

/* 【正文】视频、图片 */
.bbc-rl27ak, .bbc-189y18v, .bbc-1ka88fa, .bbc-1bh6w8m
{
display: none;
}

@page {
margin-bottom: 10;
}

CNN

1
2
3
4
5
6
7
8
9
10
#breaking-news-wrapper,
#nav,
#footer-wrap,
#story-bottom,
.ad,
.pg-rail,
.el__embedded
{
display: none;
}

The New York Times

  • 绝大多数内容需要订阅才能浏览,可在订阅窗口出现之前终止浏览器加载绕过此限制。
  • 浏览器打印输出的PDF格式友好,会自动去除广告和保持图片不会被分页截断。

https://nytimes.com/interactive/2017/06/09/movies/the-25-best-films-of-the-21st-century.html

Ctrl + Shift + P Disable JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
html, body {
overflow: auto!important;
}
.nytd-author.clearfix,
.nytd-user-comments,
h5.nytd-pullquote,
.nytd-related-promo,
.g-ad-wrapper,
#page-footer,
#related-coverage,
#BottomAd,
#navigation,
#mobile-navigation,
#gatewayCreative
{display: none !important}

纽约时报中文网

广告块较多但命名统一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.setting-bar,
.big_ad,
.article-body-aside,
.article-footer,
.nav-footer,
.download,
.article-ribbon,
#tbl-taboola-reminder,
#topad,
.title-bar {
display: none !important;
}

.article-area .article-content .article-header {
border-top: none !important;
}

The Washington Post

绝大多数内容需要订阅才能浏览,可在页面加载完后删除<html>标签中的overflow: hidden;<body>标签中的overflow: hidden;position: fixed;绕过此限制。

路透中文网

路透中文网删除少量广告内容即可有良好的打印输出

1
2
3
4
5
6
7
8
9
10
11
div[class^="AdSlot"],
div[class^="TwoColumnsLayout-leaderboard"],
div[class^="TwoColumnsLayout-right-column"],
div[class^="RelatedArticles-container-gnopT"],
nav[class="RegularHeader__nav___30a05"],
div[class^="RegularHeader__tools"],
div[class^="SocialTools__container"],
.RecircRibbon-container-cExim,
div[class^="ArticlePage-dianomi"] {
display: none;
}

日经中文网

日经中文网有独立的打印页面,便于存档

中国政府网

疾病

Cancer

中国:2015年恶性肿瘤发病约392.9万人,死亡约233.8万人。(2019年全国癌症报告

名人

  • 2003年10月,在一次例行的肾脏和输尿管检查中,医生无意中在结果上发现了乔布斯的胰脏肿瘤,由此确诊胰腺癌。
  • 1950年,从美国返回伦敦后,维特根斯坦被诊断为前列腺癌,并已扩散至骨髓。
  • 1939年9月中,佛洛伊德的下颚癌病情恶化,他忍受不了病痛的折磨,要求马克斯·舒尔按之前约定的一样帮他实施安乐死。

Diabetes

Diabetes mellitus (DM), commonly known as just diabetes, is a group of metabolic disorders characterized by a high blood sugar level over a prolonged period of time.

Tuberculosis