nginx使用介绍

2019-07-10  本文已影响0人  G__yuan

1.linux环境安装(以CentOS7为例, root用户权限)

(1).添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmact_re_model  

(2).在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来Nginx

sudo yum install -y nginx

(3).启动Nginx

sudo systemctl start nginx.service

2.windows环境安装

(1).下载地址

http://nginx.org

(2).解压

(3).启动Nginx: 运行nginx.exe

3.nginx.conf文件常用参数含义

listen:表示当前的代理服务器监听的端口,默认的是监听80端口

server_name:设置主机域名

location:表示匹配的路径

expires:用来指定过期时间

root:表示当匹配location请求的路径时,将会在root表示的路径内寻找相应的文件

proxy_pass :代理路径

注意:当匹配到location路径时,会将location路径追加到root/proxy_pass路径后面

例:location /filePreview {  

root D:/home/jenkins/qa.env.1/;

}

当匹配到/filePreview时,会在D:/home/jenkins/qa.env.1/filePreview下寻找预览文件

4.修改nginx.conf文件

(1).将server中的location删除,添加下面内容,ip需根据情况进行修改

location /CostManagement{  

proxy_pass http:// 127.0.0.1:8080; ( proxy_pass :代理路径)

}

location ~/CostManagement/assets/.*\.(html|js|css|png|gif)$ {  

root D:/apache-tomcat-8.0.33/webapps/;(root :项目在tomcat路径)

expires 30d;

}

location /filePreview {  

root D:/home/jenkins/qa.env.1/;(预览文件路径)

expires 30d;

}

(2).将server_name 的值改为服务器ip

(3).在http中添加client_max_body_size 300m;(nginx设置文件上传大小)

(4).重启nginx

5.修改后的nginx.conf文件server内容(windows环境)

http{

…… (省略掉不关注的内容)

client_max_body_size 300m;    

server {

        listen       80;

        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

location / CostManagement {  

proxy_pass http://127.0.0.1:8080;

}

location ~/CostManagement/assets/.*\.(html|js|css|png|gif)$ {  

root D:/apache-tomcat-8.0.33/webapps/;

expires 30d;

}

location /filePreview {  

root D:/home/jenkins/qa.env.1/;

expires 30d;

}

…… (省略掉不关注的内容)

}

}

6.使用nginx 反向代理访问系统

http://ip地址/CostManagement (不需要再IP地址后添加端口号)

例:http://localhost/CostManagement

安装Nginx反向代理时,如果是两台服务的话nginx会有权限问题;

解决方案:setsebool -P httpd_can_network_connect 1

Nginx挂错误页面也是有权限问题,所以把错误页面放到Nginx自己的目录下:/usr/share/nginx/html

在nginx.conf配置文件中添加如下配置信息:

upstream nktianjing{

        server 172.16.226.199:8080;

        server 172.16.226.127:8080;

        ip_hash;

}   

server {

        listen       80;

        server_name  nk.tztech.net;

#root /var/www/html;

        location / {

                #proxy_pass http://172.16.226.127:8080/;

#rewrite ^(.*)$ /index.html break; #所有请求引导到index.html

                 proxy_pass http://nktianjing;

                 proxy_set_header Host $host;

               proxy_set_header X-Real-IP $remote_addr;

        }

    }

上一篇 下一篇

猜你喜欢

热点阅读