• 技术文章 >后端开发 >php教程

    Windows下Nginx的安装及配置实例讲解

    小云云小云云2018-03-01 15:02:21原创1539

    本文主要和大家分享Windows下Nginx的安装及配置实例讲解,希望能帮助到大家。

    一、Nginx简介

    1.Nginx是什么

    Nginx是一款轻量级Web服务器、也是一款反向代理服务器

    2.Nginx能干什么

    ①可直接支持Rails和PHP的程序
    ②可作为HTTP反向代理服务器
    ③作为负载均衡服务器
    ④作为邮件代理服务器
    ⑤帮助实现前端动静分离

    3.Nginx特点

    二、Nginx安装

    1.下载:http://nginx.org/download/nginx-1.10.2.zip

    2.解压缩

    3.运行nginx.exe:通过双击图标或者cmd命令行运行

    三、Nginx常用命令

    1.测试配置文件

    安装路径下的 nginx.exe -t

    2.启动命令

    安装路径下的 nginx.exe

    3.停止命令

    安装路径下的 nginx.exe -s stop,
    或者是:nginx.exe -s quit

    4.重启命令

    安装路径下的 nginx.exe -s reload

    5.查看进程命令

    ps -ef |grep nginx

    6.平滑重启

    kill -HUP 【Nginx主进程号(即查看进程命令查到的PID)】

    7.增加防火墙访问权限

    ①sudo vim /etc/sysconfig/iptables
    ②-A INPUT -p tcp -m state –state NEW
    -m tcp –dport 80 -j ACCEPT
    ③保存退出
    ④重启防火墙 sudo service iptables restart

    四、Nginx虚拟域名配置及测试验证

    配置步骤:

    1.编辑sudo vim /usr/local/nginx/conf/nginx.conf
    ①增加include vhost/*.conf;
    ②保存退出

    nginx.conf.jpg

    2.在/usr/local/nginx/conf/目录新建vhost文件夹:
    即:/user/local/nginx/conf/vhost

    3.创建域名转发配置文件

    image.hcxjingdong.com.conf:转向目录的反向代理:
    server {
        listen 80;
        autoindex off;
        server_name image.hcxjingdong.com;
        access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
        404. html;
        if ($query_string~ * ".*[\;'\<\>].*") {
            return 404;
        }
        location~/(mmall_fe|mmall_admin_fe)/dist / view /* { 
            deny all; 
        } 
        location / { 
            root C:\ftpfile\img; 
            add_header Access-Control-Allow-Origin *; 
            } 
        }
    
    
    tomcat.hcxjingdong.com.conf:转向端口的反向代理:
    server {
        listen 80;
        autoindex on;
        server_name tomcat.hcxjingdong.com;
        access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
        404. html;
        if ($query_string~ * ".*[\;'\<\>].*") {
            return 404;
        }
        location / {
                proxy_pass http: //127.0.0.1:8080; 
                add_header Access-Control-Allow-Origin *; 
                } 
        }

    4.启动(重启)验证
    ①启动:

    {nginx}/sbin/nginx -s reload

    注:${nginx}代表安装在系统中的路径,例如:/usr/local/nginx

    5.访问验证

    使用默认80端口访问验证:http://localhost:80或http://127.0.0.1:80

    6.指向端口

    http转发

    server{
        listen 80;
        autoindex off;
        server_name learning.hcxjingdong.com;
        access_log c:/access.log combined;
        index index.html index.htm index.jsp index.php;
        #error_page 404 /404.html;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
            return 404;
        }
        location / {
            proxy_pass http://127.0.0.1:81/learning;
            add_header Access-Control-Allow-Origin *;
        }
    }

    listen 80:监听80端口;
    autoindex off:是否创建首页的索引目录;
    当nginx接到image.hcxjingdong.com(二级域名)请求,就转发到:http://127.0.0.1:81/learning目录下

    7.指向目录

    线上图片服务器,为前端提供的前端部署服务器都是通过指向目录的反向代理

    server{
        listen 80;
        autoindex off;
        server_name img.hcxjingdong.com;
        access_log c:/access.log combined;
        index index.html index.htm index.jsp index.php;
        #root /product/front/;
        #error_page 404 /404.html;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
            return 404;
        }
        location ~ /(hcxjingdong_fe|hcxmall_admin_fe)/dist/view/* {
            deny all;
        }
        location / {
            root \product\ftpfile\img;
            add_header Access-Control-Allow-Origin *;
        }
    }

    root /product/ftpfile/img:
    root直接指向硬盘系统目录product文件夹下的ftpfile下的img文件夹;
    即在访问img.hcxjingdong.com的时候就直接指向了该文件夹

    8.测试验证

    验证成功页面.jpg

    五、Nginx注意事项

    可以配置域名转发,但请一定要配置host,并且使host生效之后才可以,设置完成之后要重启浏览器

    Windows下配置:
    ①进入c:\Windows\System32\drivers\etc
    ②用记事本打开hosts文件
    ③添加好对应的域名及ip
    ④保存退出

    例如:
    10.211.55.6 image.hcx.com
    10.211.55.6 s.hcx.com

    添加域名及ip.jpg

    六、配置Windows下的Nginx

    配置hosts:
    c:\Windows\System32\drivers\etc

    配置hosts.jpg

    用浏览器访问www.hcxjingdong.com

    验证hosts配置.jpg

    包括本机访问http://localhost:

    本机访问.jpg

    配置目录的转发

    1.进入到nginx.conf(nginx的主配置):
    添加:include vhost/*.conf;

    修改nginx.conf.jpg

    2.按照该路径去创建此文件夹:
    在conf文件夹下创建vhost文件夹

    创建vhost文件夹.jpg

    3.在vhost文件夹中创建文件:image.hcxjingdong.com.conf

    创建image.hcxjingdong.com.conf.jpg

    文件内容:

    server{
        listen 80;
        autoindex off;
        server_name image.hcxjingdong.com;
        access_log c:/access.log combined;
        index index.html index.htm index.jsp index.php;
        #error_page 404 /404.html;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
            return 404;
        }
    
        location ~ /(hcxmall_fe|hcxmall_admin_fe)/dist/view/* {
            deny all;
        }
    
        location / {
            root C:\ftpfile\img;
            add_header Access-Control-Allow-Origin *;
        }
    }

    到C:\ftpfile\img目录下存放图片以便访问

    4.修改本机的host,让本机的nginx配合到image.hcxjingdong.com域名

    去到C:\Windows\System32\drivers\etc目录下修改hosts文件:

    修改本机的host.jpg

    5.重启nginx:

    进入到nginx目录执行命令:
    ①nginx.exe -t:验证配置文件是否正确
    ②nginx.exe -s reload:重启nginx

    验证并重启nginx.jpg

    6.访问域名(image.hcxjingdong.com)验证图片是否生效:

    测试host是否生效:image.hcxjingdong.com
    测试图片是否生效:http://image.hcxjingdong.com/hcx.jpg

    验证是否生效.jpg

    配置ip端口的转发

    1.在conf下的vhost下创建:tomcat.hcxjingdong.com.conf

    创建tomcat.hcxjingdong.com.conf.jpg

    使用tomcat域名进行ip端口转发,转发到tomcat服务上

    tomcat.hcxjingdong.com.conf:

    server{
        listen 80;
        autoindex off;
        server_name tomcat.hcxjingdong.com;
        access_log c:/access.log combined;
        index index.html index.htm index.jsp index.php;
        #error_page 404 /404.html;
        if ( $query_string ~* ".*[\;'\<\>].*" ){
            return 404;
        }
        location / {
            proxy_pass http://127.0.0.1:8080;
            add_header Access-Control-Allow-Origin *;
        }
    }

    2.配置hosts:

    配置hosts2.jpg

    3.启动tomcat

    4.重启nginx:nginx.exe -s reload

    5.访问http://tomcat.hcxjingdong.com
    成功显示tomcat启动页,说明http的转发也成功了。

    相关推荐:

    Windows下Nginx的安装与配置

    nginx的安装配置

    Nginx学习笔记(一):nginx的安装和配置

    以上就是Windows下Nginx的安装及配置实例讲解的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Windows Nginx 实例
    上一篇:配置php和mysql环境实现动态网站代码实例 下一篇:LNMP和zabbix配置实例代码分享
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 整理总结nginx、php-fpm和mysql等的权限划分• 十大技巧提升你的 PHP 运用实力_PHP教程• IIS7.X配置PHP运行环境小结_PHP教程• PHP网站安装程序制作的原理、步骤、注意事项和示例代码_PHP教程• php 获取远程网页内容的函数_PHP教程
    1/1

    PHP中文网