JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Centos环境下nginx主要配置方式

wys521 2024-09-18 03:07:38 精选教程 38 ℃ 0 评论

一、配置文件 /etc/nginx/nginx.conf :

vi /etc/nginx/nginx.conf

首先(这是可选的),你可以增加工作进程的数量和设置keepalive_timeout到一个合理的值:

[...]

worker_processes 4;

[...]

keepalive_timeout 2;

[...]

虚拟主机的定义 server {} 配置文件 /etc/nginx/conf.d 。

默认主机文件 (in /etc/nginx/conf.d/default.conf) 配置示例:

vi /etc/nginx/conf.d/default.conf

[...]

server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log /var/log/nginx/log/host.access.log main;

location / {

root /usr/share/nginx/html;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ .php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

location ~ .php$ {

root /usr/share/nginx/html;

try_files $uri =404;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

location ~ /.ht {

deny all;

}

}

server_name _; 使这是一个包罗万象的默认虚拟主机(当然,你可以同时喜欢在这里指定主机名 www.example.com).

在 location /部分,我们添加 index.php 到 index 行,根目录 /usr/share/nginx/html,网站文件默认目录 /usr/share/nginx/html.

在PHP中的重要组成部分是 location ~ .php$ {} 节。取消注释来启用它。改变 root 行到网站的文档根目录 (e.g. root /usr/share/nginx/html;)。请注意,我已经添加了一行 try_files $uri =404;以防止零日漏洞。 请确保您更改fastcgi_param 行为 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 否则PHP解释器不会发现你在浏览器中调用PHP脚本 ($document_root 翻译为 /usr/share/nginx/html 因为这是我们已经设置为我们的文档根目录)。

PHP-FPM 默认监听端口 9000 ,因此,我们告诉nginx的连接 127.0.0.1:9000 与行 fastcgi_pass 127.0.0.1:9000;另外,也可以使 PHP-FPM 使用Unix套接字。

现在保存文件并重新加载nginx:

# systemctl restart nginx.service

现在创建的文档根目录下的PHP探针文件 /usr/share/nginx/html…

vi /usr/share/nginx/html/info.php

<?php

phpinfo();

?>

浏览探针文件 (e.g. http://192.168.1.105/info.php):

2.让PHP-FPM使用Unix套接字

默认情况下监听端口 9000 。 另外,也可以使PHP-FPM使用Unix套接字,这避免了TCP的开销。要做到这一点,打开 /etc/php-fpm.d/www.conf…

vi /etc/php-fpm.d/www.conf

修改后如下:

[...]

;listen = 127.0.0.1:9000

listen = /var/run/php-fpm/php5-fpm.sock

[...]

然后重新加载 PHP-FPM:

# systemctl restart php-fpm.service

接下来通过你的nginx的配置和所有的虚拟主机和改线 fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock;,像这样:

vi /etc/nginx/conf.d/default.conf

[...]

location ~ .php$ {

root /usr/share/nginx/html;

try_files $uri =404;

fastcgi_pass unix:/var/run/php-fpm/php5-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

[...]

最后重新加载 nginx:

# systemctl restart nginx.service

3.反向代理配置

修改部署目录下conf子目录的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)内容,可调整相关配置。

反向代理配置示例:

location / {

#设置反向代理的地址

proxy_pass http://192.168.1.1;

#设置主机头和客户端真实地址,以便服务器获取客户端真实IP

proxy_set_header Host $host:$server_port;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#禁用缓存

proxy_buffering off;

}

代理地址根据实际情况修改。

4. 负载均衡配置

nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

负载均衡配置示例:

upstream backend {

#ip_hash;

server 192.168.1.251;

server 192.168.1.252;

server 192.168.1.247;

}

server {

listen 80;

server_name trffweb;

location / {

#反向代理的地址

proxy_pass http://backend;

}

}

Upstream命名和服务器地址根据实际情况修改。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表