首页 > 后端开发 > php教程 > Nginx配置总结

Nginx配置总结

WBOY
发布: 2016-07-29 09:15:59
原创
1148 人浏览过

修改nginx服务器配置,在conf文件下的nginx.conf文件。

<code><span>#user nobody;   #运行用户</span><span>#worker_processes 1;    #启动进程数,通常设置成和cpu数目相同</span><span>#error_log /var/log;    #错误日志</span>
events {
    worker_connections <span>1024</span>;<span>#单个后台work process进程最大并发链接数</span>
}

<span>#设定http服务器</span>
http{
     <span>#设定mime类型,类型由mime.type文件定义</span>
    include       /etc/nginx/mime.types;
    default_type  <span>application</span>/octet-stream;
    <span>#设定日志格式</span>
    access_log    /var/<span>log</span>/nginx/access.<span>log</span>;
    sendfile <span><span>on</span></span>;
    <span>#连接超时时间</span><span>#keepalive_timeout  0;</span>
    keepalive_timeout  <span>65</span>;
    tcp_nodelay        <span><span>on</span></span>;

    <span>#集群中的所有后台服务器的配置信息,用于负载均衡</span>
    upstream local_tomcat{
        server <span>192.168</span><span>.0</span><span>.111</span>:<span>80</span>;
        server <span>192.168</span><span>.0</span><span>.109</span>:<span>8080</span>;
    }
    server{
        listen <span>8090</span>;    <span>#当前代理服务器监听端口</span>
        server_name  localhost:<span>80</span>;  <span>#监听后要转到地址</span><span>#charset koi8-r;</span><span>#access_log  logs/host.access.log  main;</span>        location / {    <span>#location表示匹配路径,/表示所有请求</span>
        root   html;  <span>#静态文件存放的路径</span>
        index  index.html index.htm;  <span>#没有主页时,此为主页,可多个</span>
        }

        <span>#error_page  404              /404.html;</span><span># redirect server error pages to the static page/50x.html</span>
        error_page   <span>500</span><span>502</span><span>503</span><span>504</span>  /<span>50</span>x.html;
        location = /<span>50</span>x.html {
            root   html;
        }
    }
}
</code>
登录后复制

常见问题总结:
1. nginx+tomcat影响request值
request.getScheme() //总是 http,而不是实际的http或https
request.isSecure() //总是false(因为总是http)
request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IP
request.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URL
request.getServerName //总是nginx请求的ip
request.getServerPort //总是nginx请求的端口
response.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)
解决方法:
修改nginx配置:

<code>proxy_<span>set</span>_header Host <span>$host</span>;   <span>#解决getRequestURL、getServerName、getServerPort问题</span>
proxy_<span>set</span>_header X-Real-IP <span>$remote_addr</span>;<span>#把真实的ip发送给转发的web服务器</span>
proxy_<span>set</span>_header X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>;<span>#解决request.getScheme()只能返回http而不是https问题</span></code>
登录后复制

配置tomcat的server.xml的Engine模块,配置一个value

<code><span>Valve</span><span>className</span>=<span>"org.apache.catalina.valves.RemoteIpValve"</span><span>remoteIpHeader</span>=<span>"X-Forwarded-For"</span><span>protocolHeader</span>=<span>"X-Forwarded-Proto"</span><span>protocolHeaderHttpsValue</span>=<span>"https"</span> /></code>
登录后复制

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Nginx配置总结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板