欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安装、配置、使用nginx

程序员文章站 2022-06-11 16:35:20
...

1.使用yum install nginx命令进行安装

2.安装完成后,配置文件路径位于 /etc/nginx/nginx.conf

参考配置例子

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

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

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

server_name appgw.livibank.com 

这个是方便可以通过域名转发到不同的服务器(前提是需要配置一下host,ip为nginx所在的机器ip,域名就是server_name配置的域名)

3.nginx启动命令   nginx

4.重启命令 nginx -s reload

-------------------------------------------------------------------------------------

上面那一种是通过server_name域名匹配,还有一种根据location匹配,例如

    server {       
           listen 80;     
           location /appgw/ {           
                     proxy_pass http://192.168.0.242:8080/;           
                     proxy_set_header   Host    $host;           
                     proxy_set_header   X-Real-IP   $remote_addr;           
                     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;       
                      }  
            } 

请求时候,例如http://124.250.83.1:80/appgw/cocgw/4401,注意当转发到目标ip时候,appgw会截去,即目标机器收到的请求时

192.168.0.242:8080/cocgw/4401