Linux安装nginx

  一、安装方式1

  1、安装nginx

  sudo apt install nginx

  2、安装好的位置:

  /usr/sbin/nginx:主程序

  /etc/nginx:存放配置文件

  /usr/share/nginx:存放静态文件

  /var/log/nginx:存放日志

  3、启动并验证效果

  service nginx start #启动nginx

  service nginx reload #重新加载nginx配置文件

  nginx -s reopen # 重启 Nginx

  nginx -s stop # 停止 Nginx

  nginx -v

  二、安装方式2

  1、安装gcc依赖

  sudo apt-get install build-essentialsudo apt-get install libtoolsudo apt-get updatesudo apt-get install libpcre3 libpcre3-dev sudo apt-get install libpcrecpp0v5 libssl-dev gccsudo apt-get install zlib1g zlib1g-devsudo apt-get install openssl openssl-dev2、下载nginx下载地址1:http://nginx.org/en/download.html下载地址2:wget http://nginx.org/download/nginx-1.13.6.tar.gz解压压缩包:tar -zxvf nginx-1.13.6.tar.gz进入目录:cd nginx-1.13.6 配置产出路径: https://zhuanlan.zhihu.com/p/configure --prefix=/opt/softWare/nginx配置产出路径带ssl: https://zhuanlan.zhihu.com/p/configure --prefix=/opt/softWare/nginx --with-http_ssl_module编译:make安装:sudo make install临时启动:sudo /opt/softWare/nginx/sbin/nginx -c /opt/softWare/nginx/conf/nginx.conf创建软连接方便使用:sudo ln -s /opt/softWare/nginx/sbin/nginx /usr/bin/nginx

  查看进程

  ps -ef | grep nginx 添加ssl模块编译: https://zhuanlan.zhihu.com/p/configure --prefix=/opt/softWare/nginx --with-http_ssl_module

  make

  sudo cp https://zhuanlan.zhihu.com/p/objs/nginx /opt/softWare/nginx/sbin/

  sudo nginx -V

  configure arguments: –with-http_ssl_module说明ssl模块已安装

  三、配置文件

  3.1编译方式安装的nginx

  

  user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; log_format main '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time_local":"$time_local","reque st":"$request","sta tus":"$status","request_time":"$request_time","upstream_response_time":"$upstream_response_time","request_length":" $request_length ","bytes_sent":"$bytes_sent","body_bytes_sent":"$body_bytes_sent","gzip_ratio":"$gzip_ratio","http_referer":"$http_ referer","http_user_agent":"$http_user_agent"}'; access_log logs/access.log main; error_log logs/error.log; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; gzip on; #大于1kb才压缩 gzip_min_length 1k; gzip_buffers 4 16k; #压缩级别1-10 越大压缩越费时间越好 gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off; #去除ie6以下支持 gzip_disable "MSIE [1-6]\."; include /opt/softWare/nginx/conf/conf.d/*.conf; }

  3.1 apt 方式安装的nginx

  

  user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; # multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /opt/softWare/nginx/conf/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; log_format main '{"remote_addr":"$remote_addr","remote_user":"$remote_user","time_local":"$time_local","reques t":"$request","status":"$status","request_time":"$request_time","request_length":"$request_length","bytes_sent":"$bytes_sent","body_bytes_sent":"$body_bytes_sent","gzip_rati o":"$gzip_ratio","connection_requests":"$connection_requests","http_referer":"$http_referer","http_user_agent":" $http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for"}'; access_log /opt/softWare/nginx/log/access.log main; error_log /opt/softWare/nginx/log/error.log; #开启图片和html压缩 gzip on; #大于1kb才压缩 gzip_min_length 1k; gzip_buffers 4 16k; #压缩级别1-10 越大压缩越费时间越好 gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-ht tpd-php image/jpeg image/gif image/png; gzip_vary off; #去除ie6以下支持 gzip_disable "MSIE [1-6]\."; include /opt/softWare/nginx/conf/conf.d/*.conf; }

  

  server { listen 443 ssl; server_name www.robots2.com; ssl on;#新版本去除并443后面加ssl ssl_certificate /opt/softWare/nginx/cert/5571550_www.robots2.com.pem; ssl_certificate_key /opt/softWare/nginx/cert/5571550_www.robots2.com.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; # 上述端口指向的根目录 root /opt/project/catering_earth/litemall-admin/dist; # 项目根目录中指向项目首页 index index.html; # 根请求会指向的页面 location / { # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404 try_files $uri $uri/ @router; # 请求指向的首页 index index.html; } # 由于路由的资源不一定是真实的路径,无法找到具体文件 # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源 location @router { rewrite ^.*$ /index.html last; } location /wx/ { proxy_pass http://localhost:8082; 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 /admin/ { proxy_pass http://localhost:8083; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { # 上述端口指向的根目录 root /opt/nowProject/catering_earth/litemall-admin/dist; # 项目根目录中指向项目首页 index index.html; client_max_body_size 20m; client_body_buffer_size 128k; # 根请求会指向的页面 location / { # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404 try_files $uri $uri/ @router; # 请求指向的首页 index index.html; } # 由于路由的资源不一定是真实的路径,无法找到具体文件 # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源 location @router { rewrite ^.*$ /index.html last; } }