编程技术分享平台

网站首页 > 技术教程 正文

Nginx安装配置(nginx安装配置教程)

xnh888 2024-09-11 10:56:14 技术教程 14 ℃ 0 评论

一、Nginx安装

  • 检查安装依赖包
 # 检查zlib是否安装
 dpkg -l | grep zlib
 # 依赖包openssl安装
 sudo apt-get install openssl libssl-dev
 # 依赖包pcre安装
 sudo apt-get install libpcre3 libpcre3-dev
 # 依赖包zlib安装
 sudo apt-get install zlib1g-dev
  • 基于APT安装
 sudo apt-get install nginx

安装目录:

/usr/sbin/nginx:主程序

/etc/nginx:存放配置文件

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

/var/log/nginx:存放日志

  • 通过源码包安装
 #下载最新版本:
 wget http://nginx.org/download/nginx-1.11.3.tar.gz
 #解压:
 tar -zxvf nginx-1.11.3.tar.gz
 #进入解压目录:
 cd nginx-1.11.3
 #配置:
 ./configure --prefix=/usr/local/nginx 
 #编辑nginx:
 make
 #安装nginx:
 sudo make install
  • nginx命令
 nginx -s reload # 重新载入配置文件
 nginx -s reopen # 重启 Nginx
 nginx -s stop # 停止 Nginx

二、Nginx配置

  • nginx.conf
 user www-data;
 worker_processes auto; #nginx进程数,一般与cpu一致
 pid /run/nginx.pid; #pid路径
 events { #events块
 worker_connections 768; #单个后台woker process最大并发连接数
 # multi_accept on;
 }
 http {
 ##
 # Basic Settings
 ##
 sendfile on; #高效传输模式开启
 tcp_nopush on; #开启tcp_nopush可以允许把httpresponse header和文件的开始放在一个文件里发布,减少网络报文段数量
 tcp_nodelay on; #内核会等待将更多的字节组成一个数据包,从而提高I/O性能
 keepalive_timeout 65; #链接超时时间,秒
 types_hash_max_size 2048; #影响散列表的冲突率。越大消耗内存越多,但散列key的冲突率会降低,检索速度快
 # server_tokens off; #隐藏相应header和错误通知中的版本号
 # server_names_hash_bucket_size 64; #设定请求缓存
 include /etc/nginx/mime.types; #设定mime类型,类型由mime.types文件定义
 default_type application/octet-stream;
 ##
 # SSL Settings
 ##
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用安全协议,禁止其他不安全的ssl协议
 ssl_prefer_server_ciphers on; #设置服务器优先于客户端
 ##
 # Logging Settings
 ##
 access_log /var/log/nginx/access.log; #设定日志文件
 error_log /var/log/nginx/error.log; #设定错误文件
 ##
 # Gzip Settings 能够快速节省带宽、快速响应用户访问
 ##
 gzip on; #开启gzip压缩
 gzip_disable "msie6"; #IE6不进行压缩
 # gzip_vary on; #on会在header里增加"Vary:Accept-Encoding"
 # gzip_proxied any; #nginx做前端代理时启用,无论后端服务器的headers头返回什么信息,都无条件压缩
 # gzip_comp_level 6; #压缩级别,越大压缩效果越好,时间也越长
 # gzip_buffers 16 8k; #buffer
 # gzip_http_version 1.1; #HTTP/1.1
 ##
 # Virtual Host Configs 虚拟主机
 ##
 include /etc/nginx/conf.d/*.conf; #可以在conf.d文件夹下创建每个项目的配置文件
 include /etc/nginx/sites-enabled/*; #nginx默认代理
 }
 #mail反向代理
 #mail {
 # # See sample authentication script at:
 # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
 #
 # # auth_http localhost/auth.php;
 # # pop3_capabilities "TOP" "USER";
 # # imap_capabilities "IMAP4rev1" "UIDPLUS";
 #
 # server {
 # listen localhost:110;
 # protocol pop3;
 # proxy on;
 # }
 #
 # server {
 # listen localhost:143;
 # protocol imap;
 # proxy on;
 # }
 #}
  • http.conf http请求虚拟主机
 server{
 listen 80; #端口
 server_name xxxxxx.com; #域名
 access_log /log/access.log; #日志文件
 error_log 404 /404.html; #错误重定向导404.html
 location /
 {
 root /yourhome; #项目目录
 index index.html; #首页
 }
 location = /404.html #配置错误页面转向
 {
 root /yourhome/;
 index 404.html;
 }
 }
  • https.conf https请求虚拟主机
 upstream yunblog {
 server unix:/home/ubuntu/yunblog/unicorn.sock fail_timeout=0;
 }
 server { #将http请求重定向到https
 listen 80;
 server_name yunleifu.com www.yunleifu.com;
 rewrite ^(.*) https://$server_name$1 permanent;
 }
 server {
 listen 443 ssl;
 server_name yunleifu.com www.yunleifu.com;
 if ($host != 'www.yunleifu.com'){
 rewrite ^/(.*)$ https://www.yunleifu.com/$1 permanent;
 } 
 root /home/ubuntu/yunblog/public; #项目目录
 #密钥文件
 ssl_certificate cert/1_www.yunleifu.com_bundle.crt;
 ssl_certificate_key cert/2_www.yunleifu.com.key;
 location / {
 proxy_redirect off; #开启后,当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。
 proxy_set_header Host $host; # $host就是nginx代理服务器
 proxy_set_header X-Forwarded-Host $host; #事实上的标准首部,用来确定客户端发起的请求中使用的host指定的初始域名
 proxy_set_header X-Forwarded-Server $host;
 proxy_set_header X-Real-IP $remote_addr; #客户端ip
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header X-Forwarded-Proto $scheme;
 proxy_set_header X-Forwarded-Ssl on; # 开启ssl,必须写
 proxy_set_header X-Forwarded-Port $server_port;
 proxy_buffering on;
 proxy_pass http://yunblog;
 }
 }

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表