初探Nginx 之 8080端口监听 及 SSL实现 及 Thinkphp支持

1493493328

瞎扯一下生活日常,难得放假,今天睡醒便是12点整,下午撸过两个小时10倍边境的飞车之后便又找了mobike踩,也许是太久没踩了,踩完回来异常的累, 然后晚上无聊到炸,心血来潮学了下Ngnix。

本人使用的是phpStudy集成的Nginx,懒人必备。

不得不说Nginx反向代理巨好用,Nginx使用广泛,什么负载均衡啊,反向代理噼里啪啦的一大堆,之前没有去了解Nginx以为很难,当时也有朋友问过我在自己服务器装了Tomcat不想在域名加8080而又不想用apache映射到8080端口当时我就说用Nginx可以实现,其实很简单哈哈,值得深入研究。

长话短说直接进入正题,如果你使用的是phpstudy集成的,你之前在Apache配置好的域名切换到Nginx之后也会自动的帮你生成,这就是使用集成的好处哈哈哈,省事。

这是Ngnix学习的几个pdf 第1章 Nginx简介.pdf 第2章 Nginx服务器的安装与配置.pdf 第3章 Nginx的基本配置与优化.pdf

关于Ngnix配置的一些信息意思的了解pdf或者百度都有比较详细的说明,在此略过。

  1. 8080端口的映射
    首先要明白一个server就是一个虚拟主机的配置,在localhost加上一下配置信息

#所有jsp的页面均交由tomcat处理
location ~ .(jsp|jspx|do)?${ proxy_set_header Host$host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
我服务器的tomcat服务器端口没改,如果是其他的则修改为相对应的端口
然后我使用jsp.xiaocp.com域名指向,并配置一个虚拟主机

server {
listen 80;
server_name jsp.xiaocp.com:8080;

    location / {  
        root   "C:\Tomcat 7.0\webapps"; 
        index  index.jsp index.htm index.html index.do default.jsp default.do;
    } 

}

root为Tomcat应用程序根目录
server_name 是主机名称,还可以简写为 server_name jsp.xiaocp.com:;

这样,就成功配置好了,是不是很简单哈哈哈,8080端口会保留,访问 http://jsp.xiaocp.comhttp://www.xiaocp.com:8080/ 是一样的

下面是我的一个servlet servlet课题 一个springmvc springmvc课题

  1. Thinkphp的路由支持
    使用了Ngnix,对应使用的thinkphp框架路由会出现一些问题,我的挺多个项目都是基于TP开发的,我博客使用的路由模式为2,并且是自定义路由,所以默认Ngnix访问的url全都404 Not Found 了,这个可以很简单的解决,写一下rewrite规则就可以解决,配置如下

server {
listen 80;
server_name www.xiaocp.com xiaocp.com;

root   "C:/phpStudy/WWW/home";
location / {
    index  index.html index.htm index.php;

    #兼容tp路由
    if (!-e $request_filename){ 
      rewrite ^/(.*)$ /index.php/$1 last;
    }
}
location ~ \.php(.*)$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO  $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    include        fastcgi_params;
}

}
如果是其他的,可以根据url规则进行配置比如tp兼容模式配置如下
if (!-d $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last;
}

  1. SSL的配置
    切换到了Ngnix,SSL需要重新配置,也是挺简单的,监听443端口,配置如下

server {
listen 443 ssl;
server_name www.xiaocp.com;

    ssl_certificate      C:/Ape/ssl/www_ssl/2_xiaocp.com.crt;
    ssl_certificate_key  C:/Ape/ssl/www_ssl/3_xiaocp.com.key;

    #ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    ssl on;

    location / {
        root   C:/phpStudy/WWW/home;
        index  index.html index.htm index.php;
        if (!-e $request_filename){
          rewrite ^/(.*)$ /index.php/$1 last;
        }
    }

    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

    root           "C:/phpStudy/WWW/home";
}

这里有个坑,一开始是查资料加的一些配置,然后一启动Ngnix就报错,报错信息 shared zone “SSL” has no equal addresses: 02C00000 vs 02650000还是pdf资料有用哈哈哈,把这一行注掉即可ssl_session_cache shared:SSL:1m;

然后就是http 重定向到 https 了

在原来的虚拟主机配置加上

return 301 https://$server_name$request_uri;
perfect,完美解决。
好吧,为了写这篇鬼东西,比我去学Ngnix还花时间,可以安心去睡觉了