腾讯云centos7 环境搭建(2)nginx安装

安装

yum install nginx

安装之后,可以查看nginx的默认安装目录

[[email protected]_0_9_centos ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz

以下是Nginx的默认路径:

(1) Nginx配置路径:/etc/nginx/
(2) PID目录:/var/run/nginx.pid
(3) 错误日志:/var/log/nginx/error.log
(4) 访问日志:/var/log/nginx/access.log
(5) 默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到

设置自动启动

# 设置自动启动
systemctl enable nginx

[[email protected]_0_9_centos nginx]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service

# 启动nginx
systemctl start nginx

nginx配置

在真实的环境中,通常一个nginx会有多个站点的配置,如果所有站点的配置信息都放在nginx.conf中那将非常不便于管理和维护,为此我们可以将每个站点的配置信息放在单独的文件中,然后在nginx中include进来。
然后编辑nginx.conf为如下内容

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;

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

    #访问日志
    client_max_body_size 30m;
    client_header_buffer_size 64k;
    large_client_header_buffers 4 64k;

    gzip on;
    gzip_min_length 2k;
    gzip_comp_level 2;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    #proxy_buffering on;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 512k;
    proxy_buffers 4 512k;
    proxy_busy_buffers_size 512k;
    proxy_temp_file_write_size 512k;

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

    #开启限制IP连接数的时候需要使用
    #limit_zone crawler $binary_remote_addr 10m;

    include /etc/nginx/conf.d/*.conf;

# 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 {
#        }
#    }
}

www.goodapp.net的配置文件www.goodapp.net.conf的内容

 server {
    listen 80;
    server_name www.goodapp.net;
    index default;
    root /tmp;
    error_page                      400 402 403 404 405 406 410 411 413 416 500 501 502 503 504 /error.html;
    error_page                      505 /error.html;

    #定义本虚拟主机的访问日志
    access_log /var/log/nginx/dev.log main;
    error_log  /var/log/nginx/dev_error.log;

    location / {
        proxy_pass              http://127.0.0.1:8080/dev/;
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Content-Type    $http_content_type;
        proxy_set_header        Referer $http_referer;
        proxy_set_header        User-Agent $http_user_agent;
    }
    #location /gateway/ {
    #   proxy_pass http://172.30.43.244:18003/;
    #    proxy_set_header        Host $host;
    #    proxy_set_header        X-Real-IP $remote_addr;
    #    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    #    proxy_set_header        Content-Type    $http_content_type;
    #    proxy_set_header        Referer $http_referer;
    #    proxy_set_header        User-Agent $http_user_agent;
    #    proxy_set_header        apiName $http_apiName;
    #    proxy_set_header        apiUrl  $http_apiUrl;
    #    proxy_set_header        apiVersion      $http_apiVersion;
    #    proxy_set_header        jtoken  $http_jtoken;
    #    proxy_set_header        verifyCode      $http_verifyCode;
    #}
}
文章来源: 腾讯云centos7 环境搭建(2)nginx安装

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:腾讯云centos7 环境搭建(2)nginx安装