分享,  系统

Linux安装Nginx

安装 nginx

cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.18.0.tar.gz
sudo tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
sudo ./configure
sudo make
sudo make install
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

默认配置

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

添加 nginx 用户

sudo groupadd nginx
sudo useradd -g nginx nginx

创建日志文件夹

cd /data
sudo mkdir logs
sudo chown -R nginx:nginx ./logs

配置 nginx

主目录:/usr/local/nginx

nginx.conf

user nginx nginx;
worker_processes auto;

error_log /data/logs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 100m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;


  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";


######################## default ############################
  server {
    listen 80;
    server_name _;
    access_log /data/logs/access_nginx.log combined;
    root html;
    index index.html index.htm;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #location /nginx_status {
    #  stub_status on;
    #  access_log off;
    #  allow 127.0.0.1;
    #  deny all;
    #}

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico){
      expires 30d;
      access_log off;
    }

    location ~ .*\.(js|css)? {
      expires 7d;
      access_log off;
    }

    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }
########################## vhost #############################
  include vhost/*.conf;
}

vhost/learnzs.com.conf

server {
  listen 80;
  listen [::]:80;
  server_name learnzs.com;
  access_log /data/logs/learnzs.com_nginx.log combined;
  index index.html index.htm index.jsp;
  root /data/apps/learnzs.com;

  #error_page 404 /404.html;
  #error_page 502 /502.html;

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico){
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)? {
    expires 7d;
    access_log off;
  }
  location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
    deny all;
  }
  location ~ {
    proxy_pass http://127.0.0.1:8080;
    include proxy.conf;
  }
}

proxy.conf

proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer http_referer;
proxy_set_header Cookiehttp_cookie;
proxy_set_header Host host;
proxy_set_header X-Real-IPremote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Protoscheme;

nginx 命令

#启动
sudo nginx
#重启
sudo nginx -s reload
#关闭
sudo nginx -quit

开机启动 nginx

sudo vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[Unit] 服务的说明
Description 描述服务
After 描述服务类别
[Service] 服务运行参数的设置
Type=forking 是后台运行的形式
ExecStart 为服务的具体运行命令
ExecReload 为重启命令
ExecStop 为停止命令
PrivateTmp=True 表示给服务分配独立的临时空间
#开机自启动
systemctl enable nginx
#关闭开机自动启动
systemctl disable nginx
#启动nginx服务
systemctl start nginx.service
#停止服务
systemctl stop nginx.service
#重新启动服务
systemctl restart nginx.service
#查看服务当前状态
systemctl status nginx.service
#查看所有已启动的服务
systemctl list-units --type=service

发表评论

邮箱地址不会被公开。