nginx安装与常用配置
本文最后更新于 1026 天前,其中的信息可能已经有所发展或是发生改变。

安装并配置 nginx。

1. 安装

安装以 centos7 为例。

安装 epel 源。以及安装 yum-utils,其 内部的 yum-config-manager 可以用来更改某些源中软件的默认安装版本:

yum install epel-release yum-utils -y

安装 nginx:

yum install -y nginx

# now 参数指定 enable 的同时 start,即完成开机自启及立即启动
systemctl enable nginx --now

如果是云服务器,还需要去控制台安全组开放服务器的 80 和 443 端口。

2. 配置

nginx 配置文件都在 /etc/nginx 下。

2.1 vim nginx高亮

先使 vim 支持 nginx 语法高亮,高亮配置使用 github repo: chr4/nginx.vim

# 下载与移动配置文件
git clone https://github.com/chr4/nginx.vim
mkdir ~/.vim
mv ./nginx.vim/syntax ~/.vim
rm -rf ./nginx.vim

# 关联 nginx 配置文件
cat > ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/*,*/conf/nginx.conf if &ft == '' | setfiletype nginx | endif
EOF

2.2 nginx gzip 压缩

参考博客: 加速nginx: 开启gzip和缓存

修改 /etc/nginx/nginx.conf http 下的字段:

# /etc/nginx/nginx.conf
# http 注释原 gzip 配置,添加下面配置

gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

2.3 主站配置

查看 cat /etc/nginx/nginx.confhttp 字段内容为:include /etc/nginx/conf.d/*.conf;,即其会加载此路径下所有 conf 文件。为方便管理和扩展,我们在 conf.d 下写主站点的配置。

下面配置 https,并 http 自动跳转到 https。

  1. 新建配置文件:
    cd /etc/nginx
    vim conf.d/mainsite.conf
    
  2. 写入配置文件:
    server {
       listen 80;
       server_name  www.yourdomain.cn  yourdomain.cn;
       rewrite ^(.*)$    https://www.yourdomain.cn$1  permanent;
    #    location / {
    #        index index.html index.htm;
    #    }
    }
    
    server {
       listen 443 ssl;
       server_name  www.yourdomain.cn  yourdomain.cn;
    
       root /var/www/html;  # 网站跟目录
    
       index index.php index.html index.htm;
    
       ssl_certificate cert/your.pem;  #自己证书文件, pem。
       ssl_certificate_key cert/your.key;  #自己证书文件, key
       ssl_session_timeout 5m;
       ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       ssl_prefer_server_ciphers on;
    
       # 这种写法也可以防止直接访问文件夹,推荐!
       location / {
           # add_header Content-Type text/plain;  # debug
           # return 200 $host:debug;  # debug
           try_files $uri $uri/ /index.php;
       }
    
       # 防止 xmlrpc.php 攻击,对 wordpress 至关重要!
       location ~* /xmlrpc.php$ {
           allow 127.0.0.1;
           deny all;
       }
    
       # 禁止访问隐藏文件
       location ~ /\. {
           deny all;
       }
    
       location ~ .*\.php(\/.*)*$ {
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           fastcgi_pass unix:/run/php7.0-fpm.sock;  # php7.0 sock path
           fastcgi_index index.php;
           include fastcgi.conf;
       }
    
       # 引入网站根目录下的配置。用不到时建议注释。
       location ~ /nginx.conf {
           deny all;
       }
       include /var/www/html/nginx.conf;
    }
    
  3. 证书,上面配置中证书路径为 /etc/nginx/cert,申请证书后放在这里即可,如我将证书放在 /etc/nginx/cert/mainsite:
    mvdir -p cert/mainsite
    
  4. 关于 php sock 相关的配置见xxxxxxxxxxxxx

2.4 隐藏 nginx 与 php 版本号

隐藏 nginx 版本号:在 /etc/nginx/nginx.conf 文件 http 字段下添加或取消注释 server_tokens off

隐藏 php 版本号:在 php.ini 文件中修改 expose_phpOff

php.ini 文件目录,通常在 /etc/php/{版本号}/fpm/php.ini,可以通过建立下面文件让客户端访问来查看配置文件的目录。

<?php
echo phpinfo();

3. 反代理其他服务

3.1 反代理 jupyterhub 到二级域名

配置 jupyterhub #3.5 nginx 反代理

3.2 反代理 frp 到三级域名

frp内网穿透以及nginx反代理

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇