1.nginx反向代理后,css、js、jpg等资源加载不出来

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
user nobody;
worker_processes 4;
events{
worker_connections 1024;
}
http{
upstream xd-project{
server 47.52.68.xx:8080;
server 45.40.205.xx;
}
server{
listen 80;
location / {
proxy_pass http://xd-project;

}

location ~ .* {
proxy_pass http://xd-project;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

2.nginx代理hexo博客

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
1. 在服务器端,
a. 在用户目录下创建git仓库,假设目录为 /root/blog,git初始化命令,git init blog.git
b. 在blog.git/hook目录下新增文件 post-receive,内容如下
#!/bin/bash
git --work-tree=/var/www/blog --git-dir=/root/blog/qzwjer.git checkout -f
c. 将post-receive赋予访问和运行权限
d. 创建博客访问目录,在 /var/www 下创建 blog,此目录将为博客页面目录
e. 修改nginx配置文件,在/etc/nginx/sites-avaliable/default,增加location匹配项:
location ^~ /blog {
root /var/www/;
}
f. nginx -s reload

2. 在本地,
a. 将ssh公钥添加到服务器,ssh公钥在.ssh目录下,如果没有则生成
ssh-keygen -t rsa //生成
ssh-copy-id -i .ssh/id_rsa.pub root@118.xxx.xxx.xxx
b. 上传公钥后,可直接 ssh root@118.xxx.xxx.xxx 登录服务器,或在.ssh/config 文件中添加
Host abc
HostName 118.xxx.xxx.xxx
User root
然后 ssh abc 即可登录
c. git clone root@118.xxx.xxx.xxx:/root/blog/blog.git (不用)
d. 修改hexo博客 _config.yml 文件,在deploy repo下添加服务器git库,
- root@118.xxx.xxx.xxx:/root/blog/blog.git
e. 在_config.yml文件中url行下添加一行
root: /blog
此行为所有资源文件添加根路径blog,匹配服务器nginx的配置路径
f. hexo clean, hexo g, hexo d

3.nginx设置访问服务器目录

1
2
3
4
5
6
7
8
9
10
11
12
13
1. 在服务器上添加一个目录,用于nginx访问。(/root/doc)
2. 配置 /etc/nginx/sites-avaliable/default,增加location匹配:
location ^~ /doc {
alias /root/doc;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
3. 在设置完后,发现nginx 403 forbidden,经过一番查找后,修改 /etc/nginx/nginx.conf文件,
将 user www-data; 改为 user root;
后即可正常访问
4. 发现markdown文件点击后直接下载,而不能预览,在/etc/nginx/mime.types 文件中未发现md,将其加到txt一行。

4.nginx查看错误日志

1
2
nginx的错误日志路径在 /etc/nginx/nginx.conf文件中,
error_log /var/log/nginx/error.log