在Ubuntu上安装Nginx

少于 1 分钟读完

nginx 的安装, 日志文件、配置文件路径, 端口转发配置,启动命令等。

安装

安装依赖包

   sudo apt install curl gnupg2 ca-certificates lsb-release

设置nginx仓库

   echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

添加签名

   curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add 

签名校验,输出应该包含:573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62

   sudo apt-key fingerprint ABF5BD827BD9BF62

安装nginx

   sudo apt update
   sudo apt install nginx

文件路径

log file

error.log and access.log

  • /usr/local/nginx/logs
  • /var/log/nginx

pid file

nginx.pid

  • /var/local/nginx/logs
  • /var/run

配置文件

nginx.conf

  • /user/local/nginx/conf
  • /etc/nginx
  • /usr/local/etc/nginx

配置

配置端口转发与SSL

server {

     listen 443;

     server_name www.sevendark.cn;
     ssl on;
     ssl_certificate conf.d/Nginx/1_www.sevendark.cn_bundle.crt;
     ssl_certificate_key conf.d/Nginx/2_www.sevendark.cn.key;
     ssl_session_timeout 5m;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
     ssl_prefer_server_ciphers on;

     location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
     }

     location /km {
         # for support csrf
         proxy_set_header        Host            $host;
         proxy_set_header        X-Real-IP       $remote_addr;
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass_header       Set-Cookie;
         # proxy port
         proxy_pass http://127.0.0.1:8080/km;
     }

     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }

配置80转发443

server {
    listen 80;
    server_name www.sevendark.cn;
    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}

配置最大Request Body 在 /etc/nginx/nginx.conf 文件中

http {
  ...
  client_max_body_size 100M;
}

命令

nginx -s reload
nginx -s quit

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

文档

🔶 nginx install doc

标签:

分类:

更新时间: