nginx 简单配置

来源:互联网 发布:xampp mac 安装教程 编辑:程序博客网 时间:2024/05/21 14:01


 



首先,必须确保下载到正确的nginx安装包,这里先采用1.9.14版本的安装包。
[root@lamp mnt]# tar zxf nginx-1.9.14.tar.gz
[root@lamp mnt]# ls
nginx-1.9.14  nginx-1.9.14.tar.gz
[root@lamp mnt]# cd nginx-1.9.14
[root@lamp nginx-1.9.14]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@lamp nginx-1.9.14]# cd src/core/
[root@lamp core]# ls
nginx.c           ngx_inet.h             ngx_radix_tree.h
nginx.h           ngx_list.c             ngx_rbtree.c
ngx_array.c       ngx_list.h             ngx_rbtree.h
ngx_array.h       ngx_log.c              ngx_regex.c
ngx_buf.c         ngx_log.h              ngx_regex.h
ngx_buf.h         ngx_md5.c              ngx_resolver.c
ngx_conf_file.c   ngx_md5.h              ngx_resolver.h
ngx_conf_file.h   ngx_module.c           ngx_rwlock.c
ngx_config.h      ngx_module.h           ngx_rwlock.h
ngx_connection.c  ngx_murmurhash.c       ngx_sha1.h
ngx_connection.h  ngx_murmurhash.h       ngx_shmtx.c
ngx_core.h        ngx_open_file_cache.c  ngx_shmtx.h
ngx_cpuinfo.c     ngx_open_file_cache.h  ngx_slab.c
ngx_crc32.c       ngx_output_chain.c     ngx_slab.h
ngx_crc32.h       ngx_palloc.c           ngx_spinlock.c
ngx_crc.h         ngx_palloc.h           ngx_string.c
ngx_crypt.c       ngx_parse.c            ngx_string.h
ngx_crypt.h       ngx_parse.h            ngx_syslog.c
ngx_cycle.c       ngx_parse_time.c       ngx_syslog.h
ngx_cycle.h       ngx_parse_time.h       ngx_thread_pool.c
ngx_file.c        ngx_proxy_protocol.c   ngx_thread_pool.h
ngx_file.h        ngx_proxy_protocol.h   ngx_times.c
ngx_hash.c        ngx_queue.c            ngx_times.h
ngx_hash.h        ngx_queue.h
ngx_inet.c        ngx_radix_tree.c
[root@lamp core]# vim nginx.h
#define NGINX_VER       "westos/"
[root@lamp core]# pwd
/mnt/nginx-1.9.14/src/core
[root@lamp core]# cd ..
[root@lamp src]# cd ..
[root@lamp nginx-1.9.14]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@lamp nginx-1.9.14]# cd auto/
[root@lamp auto]# ls
cc          have          init     module   os       threads
define      have_headers  install  modules  sources  types
endianness  headers       lib      nohave   stubs    unix
feature     include       make     options  summary
[root@lamp auto]# cd cc
[root@lamp cc]# ls
acc  bcc  ccc  clang  conf  gcc  icc  msvc  name  owc  sunc
[root@lamp cc]# vim gcc
#CFLAGS="$CFLAGS -g"
[root@lamp cc]# cd ..
[root@lamp auto]# cd ..
[root@lamp nginx-1.9.14]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@lamp nginx-1.9.14]# ./configure --prefix=/opt/lnmp/nginx --with-http_ssl_module --with-http_sub_module
[root@lamp nginx-1.9.14]# make && make install
#[root@lamp nginx-1.9.14]# make install
[root@lamp nginx-1.9.14]# cd /opt/lnmp
[root@lamp lnmp]# ls
nginx
[root@lamp lnmp]# cd nginx/
[root@lamp nginx]# ls
conf  html  logs  sbin
[root@lamp nginx]# cd conf/
[root@lamp conf]# vim nginx.conf
user nginx;
events {
     use epoll;
           worker_connections 1024;
}
[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful

以下这种情况就是没有关闭之前的进程导致的

[root@lamp conf]# nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
^C
[root@lamp conf]# ps aux | grep nginx
root      4145  0.0  0.1  47940  1108 ?        Ss   23:11   0:00 nginx: master process nginx
nobody    4146  0.0  0.2  50464  2196 ?        S    23:11   0:00 nginx: worker process
root      6775  0.0  0.0 112656   948 pts/1    R+   23:24   0:00 grep --color=auto nginx
[root@lamp conf]# kill -9 4145
[root@lamp conf]# kill -9  4146
[root@lamp conf]# nginx
[root@lamp conf]# curl -I localhost
HTTP/1.1 200 OK
Server: westos/
Date: Sat, 04 Jun 2016 03:25:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 04 Jun 2016 03:22:52 GMT
Connection: keep-alive
ETag: "5752498c-264"
Accept-Ranges: bytes

[root@lamp conf]# cd ..
[root@lamp nginx]# ls
client_body_temp  html        sbin
conf              logs        scgi_temp
fastcgi_temp      proxy_temp  uwsgi_temp
[root@lamp nginx]# cd ..
[root@lamp lnmp]# du -sh nginx/
924K    nginx/
https:
[root@lamp ~]# cd /mnt
[root@lamp mnt]# ls
nginx-1.9.14  nginx-1.9.14.tar.gz
[root@lamp mnt]# cd /opt/lnmp/nginx/
[root@lamp nginx]# ls
client_body_temp  html        sbin
conf              logs        scgi_temp
fastcgi_temp      proxy_temp  uwsgi_temp
[root@lamp nginx]# cd conf/
[root@lamp conf]# ls
fastcgi.conf            nginx.conf
fastcgi.conf.default    nginx.conf.default
fastcgi_params          scgi_params
fastcgi_params.default  scgi_params.default
koi-utf                 uwsgi_params
koi-win                 uwsgi_params.default
mime.types              win-utf
mime.types.default
[root@lamp conf]# vim nginx.conf
user nginx;
events {
     use epoll;
           worker_connections 1024;
}

    # HTTPS server
    #
  server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.pem;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

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


[root@lamp conf]# nginx -t
nginx: [emerg] BIO_new_file("/opt/lnmp/nginx/conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/opt/lnmp/nginx/conf/cert.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test failed
[root@lamp conf]# cd /etc/pki/
[root@lamp pki]# ls
CA        consumer     java   product  rsyslog
ca-trust  entitlement  nssdb  rpm-gpg  tls
[root@lamp pki]# cd tls/
[root@lamp tls]# ls
cert.pem  certs  misc  openssl.cnf  private
[root@lamp tls]# cd certs/
[root@lamp certs]# ls
ca-bundle.crt        make-dummy-cert  renew-dummy-cert
ca-bundle.trust.crt  Makefile
[root@lamp certs]# pwd
/etc/pki/tls/certs
[root@lamp certs]# vim  Makefile
[root@lamp certs]# nginx -t
nginx: [emerg] BIO_new_file("/opt/lnmp/nginx/conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/opt/lnmp/nginx/conf/cert.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test failed
[root@lamp certs]# make cert.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
cat $PEM1 >  cert.pem ; \
echo ""    >> cert.pem ; \
cat $PEM2 >> cert.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
..................................+++
...............+++
writing new private key to '/tmp/openssl.HMj9tb'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:Linux
Common Name (eg, your name or your server's hostname) []:lamp.westos.com
Email Address []:westos@westos.com
[root@lamp certs]# ls
ca-bundle.crt        cert.pem         Makefile
ca-bundle.trust.crt  make-dummy-cert  renew-dummy-cert
[root@lamp certs]# cp cert.pem /opt/lnmp/nginx/conf/cert.pem
[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload
[root@lamp conf]# netstat -antple | grep nginx
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          90836      6777/nginx: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          88608      6777/nginx: master  

打开火狐浏览器,访问:https://172.25.254.105/
下载证书 后查看证书,显示:
Welcome to nginx!

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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


虚拟机中:
[root@lamp conf]# vim nginx.conf
 server {
          listen 80;
          server_name www.westos.com;
          location / {
                  root /virtual/westos/html;
                  index index.html;
           }
     }

   server {
          listen 80;
          server_name www.linux.com;
          location / {
                  root /virtual/linux/html;
                  index index.html;
           }
     }

[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# mkdir -p /virtual/westos/html
[root@lamp conf]# mkdir -p /virtual/linux/html
[root@lamp conf]# echo www.westos.com > /virtual/westos/html/index.html
[root@lamp conf]# echo www.linux.com > /virtual/linux/html/index.html
[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload

在真机中进行解析:
[root@foundation5 ~]# vim /etc/hosts
172.25.254.105  www.westos.com
172.25.254.105  www.linux.com

以上设置完成后,可以打开一个浏览器,比如火狐浏览器,访问:http://www.linux.com/,便可以看到如下显示

www.linux.com
当访问:http://www.westos.com/,便可以看到如下显示:
www.westos.com


[root@lamp conf]# vim nginx.conf
location /message {
            stub_status on;
            access_log off;
        }

[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload
nginx: [error] invalid PID number "" in "/opt/lnmp/nginx/logs/nginx.pid"
[root@lamp conf]# killall -9 nginx
[root@lamp conf]# ps aux | grep nginx
[root@lamp conf]# nginx

打开火狐浏览器,访问:http://172.25.254.105/message/,可以看到:


Active connections: 1
server accepts handled requests
 11 11 7
Reading: 0 Writing: 1 Waiting: 0


[root@lamp conf]# vim nginx.conf
server {
          listen 80;
          server_name login.linux.com;
          location / {
                  root /virtual/login/html;
                  index index.html;
           }
     }

[root@lamp conf]# mkdir -p /virtual/login/html
[root@lamp conf]# echo login.linux.com > /virtual/login/html/index.html
[root@lamp conf]# nginx -s reload
真机中进行解析:
[root@foundation5 ~]# vim /etc/hosts
172.25.254.105  login.linux.com

此时在浏览器中访问:http://login.linux.com/,可以看到
login.linux.com

[root@lamp conf]# vim nginx.conf
server {
          listen 80;
          server_name login.linux.com;
          rewrite ^(.*)$ https://$host$1 permanent;
          location / {
                  root /virtual/login/html;
                  index index.html;
           }
     }


[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload
在浏览器中访问:https://login.linux.com
下载证书   
[root@lamp conf]# vim nginx.conf
 server {
    listen       443 ssl;
    server_name  login.linux.com;

    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.pem;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   /virtual/login/html;
        index  index.html index.htm;
    }
}
[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload
此时浏览器中访问:
https://login.linux.com

可以看到:

login.linux.com


设置轮循:

[root@lamp conf]# vim nginx.conf
http {

         upstream westos {
              server 172.25.254.105:80;
              server 172.25.254.104:80;
     }
  server {
          listen 80;
          server_name www.westos.com;
          location / {
         proxy_pass http://westos;
      }
     }
[root@lamp conf]# nginx -s reload

在虚拟机中写一个测试页:

[root@lamp conf]# vim /opt/lnmp/nginx/html/index.html

<h1>Welcome to nginx! server105</h1>

然后在浏览器中访问:http://172.25.254.105

此时,可以看到:

Welcome to nginx! server105

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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

当访问:http://www.westos.com/时,可以看都server105和server104轮循 (105 和 104 轮循)


Welcome to nginx! server105

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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


Welcome to nginx! server104


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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

设置权重:nginx

[root@lamp conf]# vim nginx.conf

http {

         upstream westos {
              server 172.25.254.105:80 weight=3;
              server 172.25.254.104:80;
     }

[root@lamp conf]# nginx -s reload

此时用浏览器访问:http://www.westos.com/

会看到每出现三次server105后会出现一次server104


Welcome to nginx! server105 (3次)


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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
Welcome to nginx! server104 (1次)

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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

用哈希绑定某个服务器:

[root@lamp conf]# vim nginx.conf

http {

         upstream westos {
              ip_hash;
              server 172.25.254.105:80;
              server 172.25.254.104:80;
     }

[root@lamp conf]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@lamp conf]# nginx -s reload

当用浏览器访问:http://www.westos.com/

会看到:

Welcome to nginx! server105


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

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 

1 0