Nginx搭建

来源:互联网 发布:淘宝买汽车配件 编辑:程序博客网 时间:2024/06/06 01:03

Nginx转载地址 ,其中高级配置没有配置,详细内容参考原文
Google 模块添加

第一部分—-nginx基本应用

源码编译安装nginx

  • 安装pcre软件包(使nginx支持http rewrite模块),安装openssl-devel(使nginx支持ssl)

yum install -y pcre pcre-devel openssl-devel

  • 创建用户nginx

useradd nginx
passwd nginx

  • 安装nginx

下载地址

这里增加两个Google的扩展module

git clone https://github.com/cuber/ngx_http_google_filter_modulegit clone https://github.com/yaoweibin/ngx_http_substitutions_filter_moduletar -vzxf nginx-1.11.3.tar.gz -C /usr/localcd nginx-1.11.3/./configure \--group=nginx \--user=nginx \--prefix=/usr/local/nginx \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--http-client-body-temp-path=/tmp/nginx/client_body \--http-proxy-temp-path=/tmp/nginx/proxy \--http-fastcgi-temp-path=/tmp/nginx/fastcgi \--pid-path=/var/run/nginx.pid \--lock-path=/var/lock/nginx \--with-http_stub_status_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-pcre \--add-module=../ngx_http_google_filter_module \--add-module=../ngx_http_substitutions_filter_modulemake &&make install

NGINX配置

修改配置文件/etc/nginx/nginx.conf

全局参数设置

worker_processes  1;    #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 error_log  logs/error.log;    #指定错误日志 worker_rlimit_nofile 102400;    #设置一个nginx进程能打开的最大文件数 pid        /var/run/nginx.pid; events {     worker_connections  1024;  #设置一个进程的最大并发连接数 } 

域名注册

免费域名注册,我使用的是谷歌账号注册和登录的

https://my.freenom.com/clientarea.php

我申请了两个域名,都是免费的

sgoogle.ga
sgoogle.gq

域名注册成功后,会有一个DNS提示,应该是在域名对应的DNS地址

http://www.freenom.world/zh/index.html?lang=zh

http服务相关设置

http {     include      mime.types;     default_type  application/octet-stream;     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';     access_log  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式     sendfile        on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载     gzip            on; #是否开启gzip压缩     keepalive_timeout  65;      #设置长连接的超时时间 

虚拟服务器的相关设置

    server {         listen      80;        #设置监听的端口         server_name  localhost;        #设置绑定的主机名、域名或ip地址         charset koi8-r;    #设置编码字符         location / {             root  /var/www/nginx;      #设置服务器默认网站的根目录位置             index  index.html index.htm;        #设置默认打开的文档             }         error_page  500 502 503 504  /50x.html;        #设置错误信息返回页面         location = /50x.html {             root  html;        #这里的绝对位置是/var/www/nginx/html         }     }  }

服务管理

  • 检测nginx配置文件是否正确

nginx -t

  • 启动nginx服务

nginx

  • 通过nginx -s控制nginx服务

nginx -s stop #停止服务
nginx -s quit #退出服务
nginx -s reopen #重新打开日志文件
nginx -s reload #重新加载配置文件

给网站启用Https

https使用的工具是certbot 。Https介绍和配置

配置步骤

配置完成后,顺便加个crontab,定时更新证书

1 1 1 * * /root/certbot/letsencrypt-auto renew


我在启用https的时候端口发生冲突,改了端口之后,无法正常使用,后续有时间再排查

原创粉丝点击