nginx 配置与运行,nginx配置运行


1. 运行nginx

cd /opt/app/nginx/sbin

sudo ./nginx

浏览器打开 http://localhost

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

看到这个网页,说明nginx运行正常.


重启nginx

sudo ./nginx -s reload


关闭nginx

ps -ef | grep nginx

root      3590     1  0 15:00 ?        00:00:00 nginx: master process ./nginx
nobody    3880  3590  0 15:16 ?        00:00:00 nginx: worker process

sudo kill 3590


2. 配置nginx

配置文件 /opt/app/nginx/conf/nginx.conf

worker_processes  1;  #表示工作进程的数量,一般设置为cpu的核数

events {
    worker_connections  1024; #表示每个工作进程的最大连接数
}

server块定义虚拟主机

listen 监听端口

server_name 监听域名

location /  匹配所有,因为都从/开头

root html制定查找路径文件夹名字叫html

index index.html 查找root目录下的html文件

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

安装目录为 /opt/app/nginx

示例server表示: 如果访问 http://localhost:81时候会打开 /opt/app/nginx/html/index.html文件


3. 实验

nginx服务器 master.hadoop(机器名) 192.168.1.84:80

tomcat1服务器 s91.hadoop(机器名) 192.168.1.91 :8080

tomcat2服务器 s131.hadoop(机器名)  192.168.1.131:8080

由nginx服务器负载均衡到tomcat1和tomcat2


在master.hadoop机器的nginx cnof文件增加如下内容

    upstream master.hadoop{
        server  192.168.1.91:8080;
        server  192.168.1.131:8080;
    }

    server{
        listen 80;
        server_name a.com;
        location / {
        proxy_pass         http://a.com;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }


在tomcat1和tomcat2的nginx.conf文件增添如下内容

    server{
        listen 80;
        server_name master.hadoop;
        index index.html;
        root /data0/htdocs/www;
    }

 

相关内容

    暂无相关文章