Nginx安装及设置成开机启动

nginx配置https和IP白名单

nginx安装ssl证书并配置
nginx限制IP访问

通过yum来直接安装

# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/

# update the yum index:
sudo yum check-update

sudo yum install -y openresty

openresty.repo:

[openresty]
name=Official OpenResty Open Source Repository for CentOS
baseurl=https://openresty.org/package/centos/$releasever/$basearch
skip_if_unavailable=False
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://openresty.org/package/pubkey.gpg
enabled=1
enabled_metadata=1

官方yum安装教程
其中核心包括如何加入到service,可以使用systemctl start nginx的命令
参考链接
也可以参考手动安装的过程
安装建议直接安装openresty,官方安装教程

yum install pcre-devel openssl-devel gcc curl
tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure
make
sudo make install
1、安装PCRE库
  
    # 解压文件
    tar -zxvf pcre-8.37.tar.gz
    cd pcre-8.34
    ./configure
    make
    make install

2、安装zlib库

    # 解压文件
    tar -zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make
    make install

3、解压nginx_mod_h264_streaming-2.2.7
    #解压文件
     tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

4、安装nginx

     
    unzip  nginx-1.5.3.zip
    cd nginx-1.5.3
    ./configure    --add-module=../nginx_mod_h264_streaming-2.2.7 
    make
    make install
5、配置nginx 
  cd /usr/local/nginx/conf/
   	vi nginx.conf
    	 在http增加
		loadstream ffcsvideo{
        		loadserver 192.168.34.182:7777;//流媒体管理的ip和端口
   	 	}
   	 
   	 	include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        include /etc/nginx/tmp-test.conf;


    	 在server增加
        	 location ~\.mp4$ {
          	      mp4;
         	       load_proxy_pass http://ffcsvideo;
       		 }
    	修改server 的root目录,跟流媒体服务的目录一致
   启动nginx
      ../sbin/nginx

nginx加载自定义的conf文件,在nginx.conf里中的http加入

		include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        include /etc/nginx/tmp-test.conf;

编译安装的nginx放到全局里执行:

vim ~/.bash_profile
alias nginx='/usr/local/nginx/sbin/nginx'
source ./bash_profile

开启nginx请求日志,获取请求体

开启这类日志需要确保nginx安装了echo模块:echo-nginx-module,推荐安装openresty,他自带了许多模块,如果是nginx的可以参考:github nginx安装
安装完成后在nginx.conf里添加log_format:记住需要顶格写在http下

log_format  main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"dm":$request_body"'
                      '"$http_user_agent" "$http_x_forwarded_for"';

在具体请求配置文件里开启日志:

server {
            listen              80;
            server_name         api.stkmart.com.au;
            access_log          /service/access_log/litemall_80.log  main;

最后nginx -s reload一下即可

遗留问题

某种情况下上述这样设置后没能生效,需要在具体请求配置文件里再加一句:echo_read_request_body;

server {
        listen 8083;
        server_name localhost;
        access_log          /service/logs/access.log  diy;
        location /test{
                echo_read_request_body;
                echo "hello";
                echo "the word is $dollar";
                echo $dollar;
        }
}

但是问题是有服务器加了这个后nginx无法再解析请求

nginx转发路径

server {
            listen    80;
            server_name    192.168.188.149;
            charset    utf-8;
 
            location / {
                proxy_pass http://192.168.188.149:81;
            }
 
            location /console {
                proxy_pass http://192.168.188.149:7001;
            }
            
            location /mytest {
              proxy_pass http://192.168.188.149:8080;
          }
 
 

访问http://192.168.188.149/ 就是iis的应用,相当于访问http://192.168.188.149:81

访问http://192.168.188.149/console 就是访问weblogic应用,相当于访问http://192.168.188.149:7001/console

访问http://192.168.188.149/mytest就是访问tomcat应用,相当于访问http://192.168.188.149:8080/mytest

如果想要达到访问http://192.168.188.149/mytest相当于访问http://192.168.188.149:8080/的效果,去掉后缀,只需要在8080后加/即可

proxy_pass http://192.168.188.149:8080;

添加模块

此处比如要添加openssl模块

#查看已有的module
nginx -V
configure arguments: --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/opt/software/pcre-8.35 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1i

将上述的nginx模块显示加入到configure命令里

./configure --with-openssl=/usr/local/openssl -with-http_ssl_module --with-http_gzip_static_module 
make

make结束后,备份原nginx可执行程序

cp /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.bak
cp -f /opt/software/nginx-1.8.1/objs/nginx /opt/nginx/sbin/nginx

安装openssl和http2

openssl安装:openssl下载

./config 
make 
make install 
make -t 
make depend 

设置环境变量/etc/profile增加export PATH=$PATH:/usr/local/ssl/bin/;然后执行source /etc/profile
nginx添加http2,openssl模块

./configure --with-openssl=/usr/local/openssl --with-http_ssl_module   --with-http_v2_module

配置nginx限制文件类型访问

这里有两个思路,一类是只允许特定文件类型访问,一类是禁止特定文件类型访问,直接看在nginx上的配置,return 403的是拒绝了以zip尾缀和带有myadmin的访问,另外一个则是只允许html,js这类的访问

server {
    listen       8091;
    server_name  localhost;

    location ~* \.(html|htm|php|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
        root   /etc/nginx/test;
        index  index.html index.htm  index.php;
        expires 3d;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
location ~ (\.zip$|myadmin) {
    return 403;
}

}

配置https

#以下属性中,以ssl开头的属性表示与证书配置有关。
server {
    listen 443 ssl;
    #配置HTTPS的默认访问端口为443。
    #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
    #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
    server_name yourdomain.com; #需要将yourdomain.com替换成证书绑定的域名。
    root html;
    index index.html index.htm;
    ssl_certificate cert/cert-file-name.pem;  #需要将cert-file-name.pem替换成已上传的证书文件的名称。
    ssl_certificate_key cert/cert-file-name.key; #需要将cert-file-name.key替换成已上传的证书私钥文件的名称。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的加密套件的类型。
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
    ssl_prefer_server_ciphers on;
    location / {
        root html;  #站点目录。
        index index.html index.htm;
    }
}

如果需要http自动跳转https:

server {
    listen 80;
    server_name yourdomain.com; #需要将yourdomain.com替换成证书绑定的域名。
    rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    location / {
        index index.html index.htm;
    }
}

配置无缓存

# kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache';
        if_modified_since off;
        expires off;
        etag off;

nginx跨域问题

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Headers' '*';
        add_header 'Access-Control-Allow-Methods' '*';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    if ($request_method = 'OPTIONS') {
        return 204;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值