Home >Backend Development >PHP Tutorial >Nginx configuration summary

Nginx configuration summary

WBOY
WBOYOriginal
2016-07-29 09:15:591157browse

Modify the nginx server configuration, the nginx.conf file under the conf file.

<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>

Summary of frequently asked questions:
1. nginx+tomcat affects the request value
request.getScheme() //always http, not actual http or https
request.isSecure() //always false (because always http)
request.getRemoteAddr() //Always the IP requested by nginx, not the user’s IP
request.getRequestURL() //Always the URL requested by nginx rather than the URL actually requested by the user
request.getServerName //Always the IP requested by nginx
request.getServerPort //Always the port requested by nginx
response.sendRedirect(relative url) //Always redirect to http (because it is considered to be an http request)
Solution:
Modify nginx configuration:

<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>

Configure the Engine module of tomcat's server.

The above has introduced a summary of Nginx configuration, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn